Sometimes, we want to read large text files line by line, without loading it into memory with Python.
In this article, we’ll look at how to read large text files line by line, without loading it into memory with Python.
How to read large text files line by line, without loading it into memory with Python?
To read large text files line by line, without loading it into memory with Python, we can use with with open.
For instance, we write
with open("log.txt") as infile:
for line in infile:
do_something_with(line)
to call open to open log.txt with with.
Then we do whatever we want to do with the file in the with block.
Using with will automatically clear the file from memory when it’s not used.
Conclusion
To read large text files line by line, without loading it into memory with Python, we can use with with open.