+ 2
If the file test.txt has 7 lines of content, what will the following expression return? len(open("test.txt").readlines())
Can any one gives the answer of this question
6 ответов
+ 5
just a comment from me:
using .readlines() function reads all lines from the file and store them in a list. this may not be an issue when the file contains just a few lines. if we have to read a file with a size of some gigabytes, a lot of memory will be used.
in this case the number of lines should be read one after the other, and counting can be done by using a counter variable:
count = 0
for line in open("books_1.txt", 'r'):
count += 1
print(count)
+ 2
for rl in file.readlines():
print(rl.replace('\n',''))
+ 2
I think I have deleted the code. I sometimes do if I have forgotten why I made it.
+ 1
It returns 7.
I made a sample with 4 lines so you can see what happens:
https://code.sololearn.com/cfOuBchfn209/?ref=app
0
7
0
Paul, your code does not open.