Why does this work with or without `.readlines()`?

what is the different from these command…?

with open("how_many_lines.txt") as lines_doc:
  for line in lines_doc.readlines():
    print(line)

and

with open("how_many_lines.txt") as lines_doc:
  for line in lines_doc:
    print(line)

both of them have same output:

1. How many lines do we write on the daily,

2. Many money, we write many many many

3. How many lines do you write on the daily,

4. Say you say many money, you write many many many

i mean, what’s actually function from .readlines() if without it, still produce same output…?

thanks …

2 Likes

I too would like to know.

If you are working with a lengthy text (millions of lines, perhaps), you might not want to upload and insert into memory a list of every line, as you would using the file.readlines() approach. for line in file:, on the other hand, loads and works with only one line at a time.

12 Likes

If not using for line in file: is more efficient with larger files, why does file.readlines() exist? for line in file: seems to get the job done faster, with less code, and be more readable.

4 Likes

Does anyone have an answer to butter_coder’s question? I’m curious as well.

1 Like

@butter_coder IANAE, but the reason for file.readlines() to exist is to create a list of lines from the file, each being stored as a string.

2 Likes

A simple test could clearly illustrate the difference.

interests.txt

I love piano.

I love painting.

Python

fr=open(‘interests.txt’)

fr.read() # ‘I love piano.\nI love painting.\n’

fr=open(‘interests.txt’)

fr.readlines() # [‘I love piano.\n’,‘I love painting.\n’]

12 Likes

@script5839156984 are you basically saying that file.readlines() isn’t necessarily meant for looping through it (though it works) but rather when you need it as a list?

1 Like

So is there major difference about the above two solutions?

1 Like

It appears that the .readlines() function generates a list of the lines. I believe the reason for line in lines_doc works with or without the .readlines() function is that for line in lines_doc.readlines() is iterating through each line in the list you made, whereas for line in lines_doc is iterating through each line in the document. .readlines() would thus be useful if trying to create and work with a list.

This can be seen by running:

lines_doc = open(‘how_many_lines.txt’)
print(lines_doc.readlines())

Which results in the list:

[‘1. How many lines do we write on the daily,\n’, ‘2. Many money, we write many many many\n’, ‘3. How many lines do you write on the daily,\n’, ‘4. Say you say many money, you write many many many\n’]

Whereas running:

lines_doc = open(‘how_many_lines.txt’)
print(lines_doc.read())

Results in:

  1. How many lines do we write on the daily,
  2. Many money, we write many many many
  3. How many lines do you write on the daily,
  4. Say you say many money, you write many many many
13 Likes

I looked into this as well and it seems that Python’s maintainers also agree that readlines() isn’t necessary for interating over the lines of a file.

Here’s a discussion about it in their bug tracker: https://bugs.python.org/issue13510

The fix that was implemented to address this concern was apparently to add the following text to the documentation:

Note that it's already possible to iterate on file objects using ``for line in file: ...`` without calling ``file.readlines()``.
5 Likes