Read all file lines into a Python list
Read lines with or without terminators, filter blank and comment lines deliberately, and avoid unnecessary memory use for large files.
Use source.readlines() to read the remaining lines of an open text file into a list. For a list without newline terminators, iterate over the file in a comprehension and remove only the characters you intend to discard.
Keep line endings
with open("file.txt", encoding="utf-8") as source:
lines = source.readlines()
Each element includes its line ending when one exists. The final line can have no terminator. With default text-mode newline handling, recognized line endings are translated to \n. list(source) is another way to collect the remaining lines.
Remove newline terminators
with open("file.txt", encoding="utf-8") as source:
lines = [line.rstrip("\n") for line in source]
This preserves spaces and tabs before the newline. Plain rstrip() also removes other trailing whitespace; strip() removes it from both ends. Choose based on the file format rather than treating all whitespace as disposable.
Filter blank and comment lines
For a format where surrounding whitespace is irrelevant and # begins a full-line comment:
lines = []
with open("file.txt", encoding="utf-8") as source:
for line in source:
text = line.strip()
if text and not text.startswith("#"):
lines.append(text)
This also skips indented comment lines. It does not parse inline comments, quoted fields, or escaped markers; use the format's parser if those rules matter.
Limit memory use
Both readlines() and a comprehension retain the complete result. For a large log, process each line directly instead:
with open("huge.log", encoding="utf-8") as source:
for line in source:
if "ERROR" in line:
print(line, end="")
Memory still depends on the longest line and any data your processing retains. Iteration does not guarantee bounded memory for arbitrarily long lines.
FAQ
How does read().splitlines() differ? It reads the whole text first, then splits it and normally removes terminators. splitlines() recognizes more Unicode line-boundary characters than ordinary file iteration, so the results can differ beyond whether newlines are retained.
How do I read only the first N lines? Use list(itertools.islice(source, n)) with a nonnegative integer. This retains at most those lines; underlying I/O may still buffer some data ahead.
How should I handle decoding errors? Prefer the known file encoding. errors="replace" replaces undecodable input; errors="ignore" silently drops it. Encoding detectors only estimate the encoding. Neither lossy mode is a substitute for preserving data when exact content matters.
References
Contact us
Join the community
to join the community
Try Guance
Start online and pay only for what you use.
Get startedChoose a Guance plan