Read a file line by line in Python
Iterate over a text file with a context manager, preserve meaningful whitespace, and handle encoding and long-line memory limits.
Open the file with a context manager and iterate over it: for line in source. This avoids loading every line into a list, and the with statement closes the file when the block ends, including when an exception occurs.
Iterate over the file
with open("file.txt", encoding="utf-8") as source:
for line in source:
print(line.rstrip("\n"))
A returned line includes its terminating newline when one is present. The final line may have none. rstrip("\n") removes trailing newline characters while preserving spaces and tabs before them.
If you simply want to copy lines to standard output, avoid stripping and adding newlines:
with open("file.txt", encoding="utf-8") as source:
for line in source:
print(line, end="")
Avoid reading everything unless it fits
with open("file.txt", encoding="utf-8") as source:
lines = source.readlines()
readlines() creates a list containing the entire file's lines. It can be convenient for small files, but line iteration is usually better for a large log. Iteration memory depends on the current line, internal buffering, and whatever your processing retains. It is not strictly constant for arbitrary input: a single extremely long line can still require substantial memory.
Skip blank lines or include line numbers
with open("file.txt", encoding="utf-8") as source:
for number, line in enumerate(source, start=1):
if line.strip():
print(number, line.rstrip("\n"))
Here, strip() only tests whether the line contains non-whitespace characters; it does not replace the original text. Use plain rstrip() instead only when removing all trailing whitespace is intentional.
FAQ
What if the encoding is not UTF-8? Specify the known encoding, such as encoding="gbk". Encoding detectors provide guesses, not proof. errors="replace" can keep a lossy diagnostic reader running, but replaces undecodable input and should not be mistaken for lossless ingestion.
How are Windows newlines handled? Default text-mode universal newline handling translates recognized line endings to \n. Binary mode or a different newline argument changes that behavior.
Can I read a file tens of gigabytes long this way? Yes, provided individual lines and your processing fit in memory. For unbounded line lengths, design a bounded chunk parser instead of assuming a line is small.
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