Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pythonic way to read file line by line?

Tags:

python

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?

like image 304
Nathan Avatar asked Nov 28 '12 03:11

Nathan


People also ask

How do I read a file line by line?

The readLine() method of BufferedReader class reads file line by line, and each line appended to StringBuffer, followed by a linefeed.

How do you read the next line in Python?

The new line character in Python is \n . It is used to indicate the end of a line of text. You can print strings without adding a new line with end = <character> , which <character> is the character that will be used to separate the lines.

How do you read multiple lines in a text file in Python?

To read multiple lines, call readline() multiple times. The built-in readline() method return one line at a time. To read multiple lines, call readline() multiple times.

Which function is used to read single line from file?

Which function is used to read single line from file? content = fh. readline().


3 Answers

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.

like image 62
g.d.d.c Avatar answered Oct 12 '22 02:10

g.d.d.c


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
like image 27
DaveP Avatar answered Oct 12 '22 01:10

DaveP


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'>
like image 1
Yunzhi Ma Avatar answered Oct 12 '22 00:10

Yunzhi Ma