+ 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

26th Nov 2022, 10:38 AM
Suchitra Patil
Suchitra Patil - avatar
6 odpowiedzi
+ 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)
26th Nov 2022, 7:34 PM
Lothar
Lothar - avatar
+ 2
for rl in file.readlines(): print(rl.replace('\n',''))
26th Nov 2022, 11:35 AM
SoloProg
SoloProg - avatar
+ 2
I think I have deleted the code. I sometimes do if I have forgotten why I made it.
22nd Feb 2023, 4:44 PM
Paul
Paul - avatar
+ 1
It returns 7. I made a sample with 4 lines so you can see what happens: https://code.sololearn.com/cfOuBchfn209/?ref=app
26th Nov 2022, 11:01 AM
Paul
Paul - avatar
0
7
19th Jan 2023, 7:47 PM
Harini Sundar
0
Paul, your code does not open.
22nd Feb 2023, 4:19 PM
Wally