liveBook · Manning

Readlines

As explained in The Quick Python Book, Fourth Edition

readlines is a file-object method that reads all remaining lines from an open stream and returns them as a list.

`readlines()` reads the remaining lines from an open file object and returns a list of strings, one per line. Trailing newline characters remain in the strings. Use it when you need the lines together, for example to count them with `len(lines)`. section 13.4

""" Reads a file and returns the number of lines, words,
    and characters - similar to the UNIX wc utility
"""
 
with open('word_count.tst') as infile: 
    lines = infile.readlines()                        #1
 
line_count = len(lines) 
word_count = sum(len(line.split()) for line in lines) #2
char_count = sum(len(line) for line in lines)         #3
 
print(f"{line_count} lines, {word_count} words, {char_count} characters")

This word counter reads the file once, then uses the list of lines to count lines, words, and characters. Because the newline characters remain in each line, `len(line)` includes them. section 8.13.2

Reading every line at once can use too much memory for a very large file. If you only need to process lines one at a time, iterate over the file object instead. `readline()` reads a single line; both `readline()` and `readlines()` also accept an optional argument affecting how much they read, with details in the Python reference documentation. section 13.4

Do not assume `infile.read().split("\n")` is interchangeable with `infile.readlines()` for character counts: splitting removes the separator characters, so summing the lengths of the resulting strings misses newlines. For text files whose original line endings must survive a read-and-write cycle, open with `newline=""` before calling `readlines()` and writing the strings with `writelines()`. section 8.13.2 section 13.4

The method also belongs to `sys.stdin`, which can be treated as a specialized file object when input is redirected. section 13.6

How The Quick Python Book, Fourth Edition covers it

How deep it goes
Defines the returned list and retained newlines, then compares memory use and alternatives; defers optional-argument details to reference documentation.
Examples
Shows line counting, a word-and-character counter, and a read-and-write example preserving line endings.
What it assumes
Assumes familiarity with opening files and basic Python lists; explains the method’s returned lines and newline behavior.
What you can do after
Apply it via Python file objects to count or reuse lines while recognizing when one-line-at-a-time processing is safer.
How it connects
Connects readlines to file iteration, readline, newline handling, string splitting, generator expressions, and writelines.

Also explained in

Compare how 2 books explain readlines →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of The Quick Python Book, Fourth Edition. The section links point to the parts of the book it draws on.