Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When should I ever use file.read() or file.readlines()?

Tags:

python

io

timeit

I noticed that if I iterate over a file that I opened, it is much faster to iterate over it without "read"-ing it.

i.e.

l = open('file','r')
for line in l:
    pass (or code)

is much faster than

l = open('file','r')
for line in l.read() / l.readlines():
    pass (or code)

The 2nd loop will take around 1.5x as much time (I used timeit over the exact same file, and the results were 0.442 vs. 0.660), and would give the same result.

So - when should I ever use the .read() or .readlines()?

Since I always need to iterate over the file I'm reading, and after learning the hard way how painfully slow the .read() can be on large data - I can't seem to imagine ever using it again.

like image 798
Maverick Meerkat Avatar asked Jun 29 '16 16:06

Maverick Meerkat


People also ask

What's the difference between read () and Readlines ()?

The main difference is that read() will read the whole file at once and then print out the first characters that take up as many bytes as you specify in the parenthesis versus the readline() that will read and print out only the first characters that take up as many bytes as you specify in the parenthesis.

What is the difference between readline () and Readlines () function in text file handling?

What is Python readline()? Python readline() method will return a line from the file when called. readlines() method will return all the lines in a file in the format of a list where each element is a line in the file.

What is the difference between readline and Readlines () functions explain with example?

The readline () function reads from a file in read mode and returns the next line in the file or a blank string if there are no more lines. (The returned data is of string type). The readlines () function also reads from a file in read mode and returns a list of all lines in the file.

What does file Readlines () do?

The readlines() method returns a list containing each line in the file as a list item. Use the hint parameter to limit the number of lines returned.


2 Answers

The short answer to your question is that each of these three methods of reading bits of a file have different use cases. As noted above, f.read() reads the file as an individual string, and so allows relatively easy file-wide manipulations, such as a file-wide regex search or substitution.

f.readline() reads a single line of the file, allowing the user to parse a single line without necessarily reading the entire file. Using f.readline() also allows easier application of logic in reading the file than a complete line by line iteration, such as when a file changes format partway through.

Using the syntax for line in f: allows the user to iterate over the file line by line as noted in the question.

(As noted in the other answer, this documentation is a very good read):

https://docs.python.org/3/tutorial/inputoutput.html#methods-of-file-objects

Note: It was previously claimed that f.readline() could be used to skip a line during a for loop iteration. However, this doesn't work in Python 2.7, and is perhaps a questionable practice, so this claim has been removed.

like image 88
Checkmate Avatar answered Oct 19 '22 20:10

Checkmate


Hope this helps!

https://docs.python.org/2/tutorial/inputoutput.html#methods-of-file-objects

When size is omitted or negative, the entire contents of the file will be read and returned; it’s your problem if the file is twice as large as your machine’s memory

Sorry for all the edits!

For reading lines from a file, you can loop over the file object. This is memory efficient, fast, and leads to simple code:

for line in f:     print line,  This is the first line of the file. Second line of the file 
like image 37
Rudi Avatar answered Oct 19 '22 19:10

Rudi