Created
November 4, 2012 13:24
-
-
Save rodriguezcommaj/4011893 to your computer and use it in GitHub Desktop.
Another Python Exercise
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # import modules | |
| from sys import argv | |
| # unpack argv | |
| script, input_file = argv | |
| # define a function that reads the passed argument | |
| def print_all(f): | |
| print f.read() | |
| # define a function that seeks the first byte in the passed argument | |
| def rewind(f): | |
| f.seek(0) | |
| # define a function that prints the passed line_count and reads the line of the passed argument f | |
| def print_a_line(line_count, f): | |
| print line_count, f.readline() | |
| # assign the opened file passed to argv to the variable current_file | |
| current_file = open(input_file) | |
| # print some text | |
| print "First let's print the whole file:n" | |
| # print the entire file | |
| print_all(current_file) | |
| # print some text | |
| print "Now let's rewind, kind of like a tape." | |
| # start at the first byte of the file | |
| rewind(current_file) | |
| # print some text | |
| print "Let's print three lines:" | |
| # set current_line to 1, print that line_count and then read that line | |
| current_line = 1 | |
| print_a_line(current_line, current_file) | |
| # increment the line, and print it | |
| current_line += 1 | |
| print_a_line(current_line, current_file) | |
| # increment the line, and print it | |
| current_line += 1 | |
| print_a_line(current_line, current_file) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment