8

What's the Pythonic way to go about reading files line by line of the two methods below?

with open('file', 'r') as f:
    for line in f:
        print line

or

with open('file', 'r') as f:
    for line in f.readlines():
        print line

Or is there something I'm missing?

0

3 Answers 3

13

File handles are their own iterators (specifically, they implement the iterator protocol) so

with open('file', 'r') as f:
  for line in f:
    # code

Is the preferred usage. f.readlines() returns a list of lines, which means absorbing the entire file into memory -> generally ill advised, especially for large files.

It should be pointed out that I agree with the sentiment that context managers are worthwhile, and have included one in my code example.

Sign up to request clarification or add additional context in comments.

Comments

7

Of the two you presented, the first is recommended practice. As pointed out in the comments, any solution (like that below) which doesn't use a context manager means that the file is left open, which is a bad idea.

Original answer which leaves dangling file handles so shouldn't be followed However, if you don't need f for any purpose other than reading the lines, you can just do:

for line in open('file', 'r'):
    print line

11 Comments

Won't this leave an open file handle?
There is really no need to use a context manager for files open for reading in scripts that aren't long-running. For writes, where you want to make sure the data made it out of the buffers when the write is done, you should always use one.
@agf -- While you're right, isn't it always a good idea to use good practices? building good habits in your short scripts can only make your more indepth scripts more robust.
@mgilson Don't code defensively. Don't write more code than you need. Those are also good habits that will serve you well in larger programs.
@agf -- Maybe. I hardly consider 1 extra line of extremely idiomatic code to be "coding defensively" or excessive in any way.
|
1

theres' no need for .readlines() method call.

PLUS: About with statement

The execution behavior of with statement is as commented below,

with open("xxx.txt",'r') as f:    
                                  // now, f is an opened file in context
    for line in f:
        // code with line

pass                              // when control exits *with*, f is closed
print f                           // if you print, you'll get <closed file 'xxx.txt'>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.