What is the difference between "in fp" and "in fp.readlines ()"?

What is the difference between for line in fp and for line in fp.readlines() ?

 with open(filename, 'r') as fp : for line in fp.readlines() : #AND with open(filename, 'r') as fp : for line in fp : 
+6
source share
2 answers

file.readlines() "[reads] and [returns] a list of lines from the stream." So, you will return to the list of all lines. Thus, the entire file is read into memory, and then broken into lines.

The documentation already says the following:

Note that it can already be iterated over file objects using for line in file: ... without calling file.readlines() .

Therefore, if you do not have the actual need to get all the lines as a list, do not use readlines . Instead, iterate over the file directly, because IOBase , which is the base type for all file handlers, implements the iterator protocol:

IOBase (and its subclasses) supports the iterator protocol, which means that the IOBase object can be repeated using strings in the stream. Strings are defined slightly differently depending on whether the stream is a binary stream (with bytes) or a text stream (with character strings). See readline() below.

Using the iterator protocol has the advantage that the file will not be fully read in memory. Instead, the file stream will be consumed iteratively and give you one line after another without the rest of the contents of the file in memory. Thus, it works very well even for very large files.

+4
source

fp - the file object itself, you can iterate over them to get the lines in the file.

Example -

 >>> f = open('test.csv','r') >>> f <_io.TextIOWrapper name='test.csv' mode='r' encoding='cp1252'> 

You can only iterate over them; you cannot directly access a specific line in a file without using seek() or such a function.

fp.readlines() - this returns a list of all lines in the file, when you repeat this, you repeat the list of lines.

Example -

 >>> f = open('test.csv','r') >>> lines = f.readlines() >>> lines ['order_number,sku,options\n', '500,GK-01,black\n', '499,GK-05,black\n', ',,silver\n', ',,orange\n', ',,black\n', ',,blue'] 

Here you can get the second line in the file using lines[1] , etc.

Usually, if the requirement is to simply iterate over the lines in the file, it is better to use file directly, since creating a list of lines and then repeating them will lead to unnecessary overhead.

+3
source

Source: https://habr.com/ru/post/989822/


All Articles