How to count total lines in a file..using file handling concept in python and is there any EOF in python like C? | Sololearn: Learn to code for FREE!
Novo curso! Todo programador deveria aprender IA generativa!
Experimente uma aula gr√°tis
+ 2

How to count total lines in a file..using file handling concept in python and is there any EOF in python like C?

I tried many times..but i was unsuccessful. How to count a line,i tried count('\n') by reading file but it doesn't work for me.

29th Jun 2019, 5:58 PM
Umesh Suman
Umesh Suman - avatar
11 Respostas
+ 3
So use this: len(text.split('\n')) That was what I was trying to say.
29th Jun 2019, 6:25 PM
őõM!N
őõM!N - avatar
+ 1
Ya hope this works for me,thanks man.
29th Jun 2019, 6:13 PM
Umesh Suman
Umesh Suman - avatar
+ 1
Excuse me. My hand got over delete section. I didn't want to do that. If you forgot my answer I can type it again.
29th Jun 2019, 6:15 PM
őõM!N
őõM!N - avatar
+ 1
Now i understand it
29th Jun 2019, 6:16 PM
Umesh Suman
Umesh Suman - avatar
+ 1
First split the text of file. And then count newline character and adding 1 gives total lines ūüėĀ
29th Jun 2019, 6:18 PM
Umesh Suman
Umesh Suman - avatar
+ 1
No no. If you want to count newlines you shouldn't split text. Just count them: text.count('\n')+1
29th Jun 2019, 6:20 PM
őõM!N
őõM!N - avatar
+ 1
No bro...i tried this in my first attempt but it doesn't count \n in file
29th Jun 2019, 6:23 PM
Umesh Suman
Umesh Suman - avatar
+ 1
It gives value 1 means text.count('\n') ==0
29th Jun 2019, 6:25 PM
Umesh Suman
Umesh Suman - avatar
+ 1
Hope this works.
29th Jun 2019, 6:26 PM
Umesh Suman
Umesh Suman - avatar
+ 1
Me, too. I can't believe that trick doesn't work. I think you better check it on different files to see what's the problem
29th Jun 2019, 6:27 PM
őõM!N
őõM!N - avatar
+ 1
Ya i try it tommorow,now it's time to go to bad for sleep.ūüėīūüė™
29th Jun 2019, 6:30 PM
Umesh Suman
Umesh Suman - avatar