Count the number of lines in a file – Python

This is a super simple tutorial. So simple, in fact, it is the shortest one I have ever written.

If you ever need to count the number of lines a file has in it and not even process the file, only count the lines, simply open the file and use a for loop to iterate over the number of lines and update an incrementer already in place. No need to even read the contents.

(View on GitHub Gist)

I told you this was a super simple and short tutorial. šŸ™‚



2 thoughts on “Count the number of lines in a file – Python

    1. OK… I did not know they had such an example in the Python docs… TIL. Plus, there are only so many ways to write such a simple script like this…

Comments are closed.