-2

Say I have a file with 4 lines. In Python, how can I check this?

Given this sudo text file

line
another line
yet another line
last line

How can I iterate through this file to find the line count? Does Python have a builtin for this?

SOLUTION: With the builtin len function I was able to read the line total, thanks.

Johnny
  • 211
  • 3
  • 15
  • see if this answers your question https://stackoverflow.com/questions/32607370/python-how-to-get-the-number-of-lines-in-a-text-file – Joe Ferndz Aug 16 '20 at 00:39

1 Answers1

2
with open('filename') as f:
    print(len(f.readlines()))

This will print the number of lines in the file, because readlines() returns a list of lines in the file

hedy
  • 1,160
  • 5
  • 23