Solving problem is about exposing yourself to as many situations as possible like Iterating on a file doesn’t work the second time and practice these strategies over and over. With time, it becomes second nature and a natural way you approach any problems in general. Big or small, always start with a plan, use other strategies mentioned here till you are confident and ready to code the solution.
In this post, my aim is to share an overview the topic about Iterating on a file doesn’t work the second time, which can be followed any time. Take easy to follow this discuss.
I have a problem with iterating on a file. Here’s what I type on the interpreter and the result:
open('baby1990.html', 'rU') for line in f.readlines(): print(line) # ... all the lines from the file appear here ...f =
When I try to iterate on the same open file again I get nothing!
for line in f.readlines(): print(line) >>>
There is no output at all. To solve this I have to
close() the file then open it again for reading! Is that normal behavior?
Yes, that is normal behavior. You basically read to the end of the file the first time (you can sort of picture it as reading a tape), so you can’t read any more from it unless you reset it, by either using
f.seek(0) to reposition to the start of the file, or to close it and then open it again which will start from the beginning of the file.
If you prefer you can use the
with syntax instead which will automatically close the file for you.
with open('baby1990.html', 'rU') as f: for line in f: print line
once this block is finished executing, the file is automatically closed for you, so you could execute this block repeatedly without explicitly closing the file yourself and read the file this way over again.
As the file object reads the file, it uses a pointer to keep track of where it is. If you read part of the file, then go back to it later it will pick up where you left off. If you read the whole file, and go back to the same file object, it will be like reading an empty file because the pointer is at the end of the file and there is nothing left to read. You can use
file.tell() to see where in the file the pointer is and
file.seek to set the pointer. For example:
open('myfile.txt') file.tell() 0 file.readline() 'onen' file.tell() 4L file.readline() '2n' file.tell() 6L file.seek(4) file.readline() '2n'file =
Also, you should know that
file.readlines() reads the whole file and stores it as a list. That’s useful to know because you can replace:
for line in file.readlines(): #do stuff file.seek(0) for line in file.readlines(): #do more stuff
lines = file.readlines() for each_line in lines: #do stuff for each_line in lines: #do more stuff
You can also iterate over a file, one line at a time, without holding the whole file in memory (this can be very useful for very large files) by doing:
for line in file: #do stuff
The file object is a buffer. When you read from the buffer, that portion that you read is consumed (the read position is shifted forward). When you read through the entire file, the read position is at the end of the file (EOF), so it returns nothing because there is nothing left to read.
If you have to reset the read position on a file object for some reason, you can do:
That is normal and sane behaviour.
Instead of closing and re-opening, you could
rewind the file.