Shengjie
Shengjie

Reputation: 12796

Is file object in python an iterable

I have a file "test.txt":

this is 1st line
this is 2nd line
this is 3rd line

the following code

lines = open("test.txt", 'r')
for line in lines:
    print "loop 1:"+line
for line in lines:
    print "loop 2:"+line

only prints:

loop 1:this is 1st line

loop 1:this is 2nd line

loop 1:this is 3rd line

It doesn't print loop2 at all.

Two questions:

  1. the file object returned by open(), is it an iterable? that's why it can be used in a for loop?

  2. why loop2 doesn't get printed at all?

Upvotes: 25

Views: 19614

Answers (5)

jamylak
jamylak

Reputation: 133624

It is not only an iterable, it is an iterator [1], which is why it can only traverse the file once. You may reset the file cursor with .seek(0) as many have suggested but you should, in most cases, only iterate a file once.

[1]: The "file" object is of class TextIOBase which is a subclass of IoBase, and IoBase supports the iterator protocol.

Upvotes: 55

Mike Müller
Mike Müller

Reputation: 85492

Your are already at the end of the file. File objects are iterators. Once you iterate over them your are at the final position. Iterating again won't start from the beginning. If you would like to start at line one again, you need to use lines.seek(0).

Upvotes: 1

Martijn Pieters
Martijn Pieters

Reputation: 1123400

Yes, file objects are iterators.

Like all iterators, you can only loop over them once, after which the iterator is exhausted. Your file read pointer is at the end of the file. Re-open the file, or use .seek(0) to rewind the file pointer if you need to loop again.

Alternatively, try to avoid looping over a file twice; extract what you need into another datastructure (list, dictionary, set, heap, etc.) during the first loop.

Upvotes: 5

Andy G
Andy G

Reputation: 19367

It would be better, though, to rewrite code so that the file doesn't need to be iterated twice. Read all the lines into a list of some kind, or perform all the processing in a single loop.

Upvotes: 0

SethMMorton
SethMMorton

Reputation: 48775

Yes, file objects are iterables but to go back to the beginning of the file you need to use lines.seek(0), since after the first loop you are at the end of the file.

Upvotes: 3

Related Questions