Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #exe 20
- #run it with something like this
- #C:\Python33>python PTHW20.py 1.txt.txt
- #
- #
- #read the errors! Typos can block you!
- from sys import argv
- #the script at the front is required but it's not a key word
- script, input_file=argv
- def print_all(f):
- f.seek(0) #to change the file object’s position, use f.seek(offset, from_what)
- #the function is called below to iterate the lines one by one
- def print_a_line(line_count,f):
- print (line_count, f.readline())
- def rewind(f):
- f.seek(0)
- #open the file that was called with input_file arg
- #eg
- #C:\Python33>python PTHW20.py 1.txt.txt
- current_file = open(input_file)
- print ("Here's the file name you wanted to open {}".format(input_file))
- print()
- print ("First, let's print the contents of the whole file:\n")
- print(current_file.read())
- print_all(current_file) #didn't work
- print()
- print ("Now, let's rewind kind of like a tape.")
- # calls the above function
- rewind(current_file)
- print ("Let's print three lines:")
- current_line = 1 #the interation worked but not current_file
- print_a_line (current_line, current_file)
- current_line = current_line +1
- print_a_line(current_line, current_file)
- current_line = current_line +1
- print_a_line(current_line, current_file)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement