Reading Large Text Files Line by Line, Memory-Efficiently
To process large text files without overwhelming your memory, we explore an approach that reads them a line at a time, without loading their entire contents.
Solution:
The key is to iterate over the file object itself, using a for loop:
with open("log.txt") as infile: for line in infile: print(line)
By using a context manager (with open(...)), we ensure the file is properly closed after processing.
How it Works:
The open() function returns a file object that supports iteration. Each line in the file is represented as a string, and the loop iterates over these lines. This allows you to process each line individually without having to load the entire file into memory.
Example Usage:
Assuming log.txt is a large text file, the code would read it line by line as follows:
with open("log.txt") as infile: for line in infile: # Perform operations on each line here (e.g., print, write to another file)
Advantages:
The above is the detailed content of How Can I Efficiently Process Large Text Files Line by Line to Avoid Memory Issues?. For more information, please follow other related articles on the PHP Chinese website!