Home / Blog / Word character & text counting

Text measurement methods

Count Lines in a Text File with Python

Count text-file lines in Python with a streaming loop, explicit encoding, blank-line rules and tests for CRLF and an unterminated final line.

Count Lines in a Text File with Python

To count lines in a text file with Python, iterate over the open file and add one for each line yielded. This reads incrementally, so it does not require loading the entire file into one string. Decide first whether blank lines belong in the total. A count of file lines is different from the number of wrapped rows displayed in an editor.

Read the file without changing it

with open("notes.txt", "r", encoding="utf-8", newline=None) as source:
    line_count = sum(1 for line in source)
print(line_count)

The read mode leaves the source unchanged. Use the actual path to your file and the encoding in which it was saved. UTF-8 is an explicit choice here, not a promise that every text file uses that encoding. If decoding fails, identify the source encoding; silently discarding undecodable bytes can conceal damaged content.

With universal newline handling, common LF, CRLF and CR line endings are recognized when reading. A final nonempty line still counts even when the file does not end with a newline character. The loop therefore avoids the common mistake of counting newline characters alone and missing that final line.

Check the boundary cases

Text represented with escapesFile linesNonblank lines
empty file00
alpha11
alpha\n11
alpha\n\nbeta32
alpha\r\nbeta\r\n22

The escaped notation describes characters in a file; it does not mean typing a backslash and the letter n into the file. A terminating newline finishes the preceding line. It does not by itself create an additional line yielded by the file iterator.

Exclude whitespace-only lines when required

with open("notes.txt", "r", encoding="utf-8", newline=None) as source:
    nonblank_count = sum(1 for line in source if line.strip())
print(nonblank_count)

This variant excludes empty lines and lines containing only whitespace. It does not remove those lines from the file. If a line containing spaces is meaningful in your format, use the first version instead. For a report, label the result as all file lines or nonblank lines so another reader can reproduce the rule.

Do not confuse lines with structured records

A CSV field can contain a quoted newline, so one CSV record may span several physical lines. To count CSV records, use a CSV parser and decide whether to exclude its header. Similarly, pretty-printed JSON and XML can use many lines for a single object. The file iterator answers a physical text-line question, not a format-specific record question.

Editor word wrapping also does not add file lines. A long paragraph may occupy ten visible rows in a narrow window and five in a wider window while remaining one file line. Resize the editor as a quick demonstration, then inspect line numbers or visible end-of-line markers if the distinction is unclear.

Verify the input and report the method

Run the small cases above in a disposable test directory before counting a large export. Use a second file with a different number of lines to confirm that the path was changed correctly. An unchanged result can be legitimate, but checking the first and last line helps detect accidentally reading an older file with the same name.

If another process is still writing the file, wait for a stable copy before making an auditable count. Record the filename, encoding and blank-line rule with the result. For a large file, this streaming approach keeps memory use small, although a single exceptionally long line still needs to be read as a line.

The browser Line Counter can cross-check copied text. Its blank-line option follows the text pasted into the page, so do not assume it preserves the original file’s line endings or its final terminator exactly.

Python reference

Python file objects and iteration describe the underlying read operation. The example counts above are independently checked fixtures.