0

I am looking for a way to efficiently find the number of lines in a text file. I am iterating through files, but each file has a different number of lines.

with open(filename) as fin:
for line in islice(fin, 5, 100000000):    
    lines.append(line)  

Instead of putting a huge number to make sure I include all the lines, is there a better way to read how many lines are in the file?

Cback17
  • 9
  • 1
  • 4
  • 2
    This seems like an XY problem to me. Rather than asking "how do I find the number of lines in a file?", you ought to be asking "how do I slice five lines off the start of a file iterator without having to specify a `stop` value?" – Kevin Jul 22 '16 at 16:44
  • Hey, @Kevin: do you think I should re-open? – mechanical_meat Jul 22 '16 at 16:45
  • count the number of line breaks – Bob Jul 22 '16 at 19:53

0 Answers0