Common Techniques for Reading Text Files in Python
Read Entire File Using open() with read()
Opening a text file and loading its full contents into memory can be done concisely using a context manager. This ensures automatic cleanup of resources.
with open('data.txt', mode='r', encoding='utf-8') as src:
whole_text = src.read()
print(whole_text)
The mode='r' flag indicates read-only access. Specifying encoding='utf-8' avoids errors when handling Unicode characters.
Fetch One Line at a Time Using readline()
To process a file sequentially one line per iteration, repeatedly call readline() until a empty string signals end-of-file.
with open('data.txt', 'r', encoding='utf-8') as src:
segment = src.readline()
while segment:
print(segment, end='')
segment = src.readline()
Using end='' in print prevents duplication of newline characters already present in the file.
Load All Lines Into a List via readlines()
Calling readlines() returns a list where each element corresponds to a line from the source file, preserving newline delimiters.
with open('data.txt', 'r', encoding='utf-8') as src:
line_list = src.readlines()
for row in line_list:
print(row, end='')
This approach is suitable when random access to specific lines is needed.
Iterate Directly Over File Object
File handles in Python are iterable, allowing concise and memory-efficient line-by-line traversal.
with open('data.txt', 'r', encoding='utf-8') as src:
for row in src:
print(row, end='')
This method avoids constructing an intermediate list, making it optimal for large files.
Treat In-Memory String as File with io.StringIO
When dealing with string data structured like file lines, io.StringIO provides a file-like interface for stream operations.
import io
sample = "alpha\nbeta\ngamma"
virtual_file = io.StringIO(sample)
for row in virtual_file:
print(row, end='')
This is useful for testing or when input originates from sources other than disk storage.