Use a for loop on a file object to read it line-by-line. Use with open(...) to let a context manager ensure that the file is closed after reading:

with open("log.txt") as infile:
    for line in infile:
        print(line)
Answer from John La Rooy on Stack Overflow
🌐
GeeksforGeeks
geeksforgeeks.org › python › how-to-read-large-text-files-in-python
How to read large text files in Python? - GeeksforGeeks
July 23, 2025 - The input() method of fileinput module can be used to read large files. This method takes a list of filenames and if no parameter is passed it accepts input from the stdin, and returns an iterator that returns individual lines from the text ...
Discussions

How do I read in a large file in chunks?
To my knowledge, you're not going to be able to thread disk io (unless you want to delve into ctypes and OS-specific functionality, see: https://docs.microsoft.com/en-us/windows/win32/sync/synchronization-and-overlapped-input-and-output?redirectedfrom=MSDN ) You can definitely thread your processing though. That can be done using normal threading: https://docs.python.org/3/library/threading.html#threading.Thread A small example: from threading import Thread from queue import Queue queue = Queue() def myFuncThatDoesStuff(q): while True: data = q.get() # process data q.task_done() for _ in range(8): t = threading.Thread(target=myFuncThatDoesStuff, args=(queue,)) t.daemon = True t.start() with open("file", "rb") as f: while data := f.read(100): queue.put(data) queue.join() More on reddit.com
🌐 r/learnpython
6
7
April 4, 2021
Any trick to load few big files in Python?
Currently I'm doing the dumb for loop and storing the data into a variable Why? Can't you just stream/fold the calculations over the data? More on reddit.com
🌐 r/Python
50
27
September 7, 2022
Non-horrible way to parse large text files?

I often have to parse large text files

How large is a "large file" ? The last person that had this problem here had a few thousand lines.

Post an example file and one of your programs that parses it and someone can tell you what you are doing wrong. Otherwise, who the hell knows.

for line in file runs at about 1,500,000 lines/second on my laptop. so no, python is not slow.

More on reddit.com
🌐 r/learnpython
14
6
July 25, 2017
Best way to process manipulate large files without reading entire file into memory
Create a generator function for it More on reddit.com
🌐 r/learnpython
3
1
January 15, 2022
🌐
DigitalOcean
digitalocean.com › community › tutorials › read-large-text-files-in-python
How to Read Large Text Files in Python | DigitalOcean
August 3, 2022 - Technical tutorials, Q&A, events — This is an inclusive place where developers can find or lend support and discover new ways to contribute to the community.
🌐
Medium
medium.com › @4sploit › reading-large-files-in-python-3bfb63b3871c
Reading large files in python. What will you learn? | by Mahmod Mahajna | Medium
November 22, 2022 - Let’s suppose we have a text file named words, this file contains some random words separated by new line and we’re willing to find the longest word and print it out, let’s also suppose that the file size is 100 mb. ... # this generator function returns a generator in each yield hit def read_words(filename): file = open(filename, "r") for line in file: yield line # the file won't close automatically since # we don't use with statement thus we need to explicitly close it file.close() def find_max_word() -> str: max_word_len = 0 max_word = None # since the generator function returns a generator # and the generator is a special type of iterator # we can simply iterate over it using for ..
🌐
Medium
medium.com › @tubelwj › how-to-read-extremely-large-text-files-in-python-cddc7dbce9fc
How to Read Extremely Large Text Files in Python | by Gen. Devin DL. | Medium
January 2, 2024 - One common approach is to use the standard file reading process in Python, which involves opening the file with the open() function and then using the readline() or readlines() methods to read the file content line by line.
🌐
Envato Tuts+
code.tutsplus.com › home › python
How to Read Extremely Large Text Files Using Python | Envato Tuts+
August 22, 2022 - Our code for reading the text file could be made more secure and readable by using the with statement in Python. This way, the context managers will automatically take care of freeing up resources once the file no longer needs to be read. But say that we wanted to directly navigate through the text file without extracting it line by line and sending that to another text file, especially since this way seems more flexible. Although the above step allowed us to read large text files by extracting lines from that large file and sending those lines to another text file, directly navigating through the large file without the need to extract it line by line would be a preferable idea.
🌐
Ubiq BI
ubiq.co › home › how to read large file in python
How to Read Large File in Python - Ubiq BI
November 29, 2024 - Generally, files are read in Python using readlines() method, which loads the entire file in memory. It returns a list where each element is a line of the file. This list is stored in memory.
Find elsewhere
🌐
Reddit
reddit.com › r/learnpython › how do i read in a large file in chunks?
r/learnpython on Reddit: How do I read in a large file in chunks?
April 4, 2021 -

Hello,

I would like to know how to best approach the following task.

I have a large file which is a few million lines. I want to read each line and do something with it.

Considerations:

  • I don’t want the whole file loaded into memory at once, I want this loaded in chunks

  • Threading should be used (unless there’s a better option)

My initial thought process in pseudocode would be something like:

lines_at_once = 100000

for line in read(file, lines_at_once):
    do_something(line)
🌐
LabEx
labex.io › tutorials › python-how-to-read-large-files-efficiently-434795
Python - How to read large files efficiently
This tutorial explores advanced techniques to read massive files while minimizing memory consumption and maximizing performance, providing practical strategies for handling large datasets effectively. File reading is a fundamental operation in Python programming, essential for processing data from external sources.
🌐
Medevel
medevel.com › reading-and-processing-large-files-in-python
Reading and Processing Large Files in Python
February 4, 2024 - with open('large_file.txt', 'r') as file: for line in file: process(line) # Replace 'process' with your actual processing logic · import pandas as pd chunk_size = 50000 # Adjust based on your memory constraints for chunk in pd.read_csv('large_file.csv', chunksize=chunk_size): process(chunk) # Your processing logic here
🌐
DevGenius
blog.devgenius.io › 10-ways-to-work-with-large-files-in-python-effortlessly-handle-gigabytes-of-data-aeef19bc0429
10 Ways to Work with Large Files in Python: Effortlessly Handle Gigabytes of Data! | by Aleksei Aleinikov | Dev Genius
December 1, 2024 - Reading a file line by line ensures only a small portion of the file is loaded into memory at any given time. Here’s how to do it: with open('large_file.txt', 'r') as file: for line in file: process(line) # Replace with your processing function · Why it works: Python treats the file object as an iterator, buffering small chunks of the file.
🌐
Netalith
netalith.com › blogs › python-tutorial › how-to-read-large-text-files-in-python
Read Large File in Python: Memory-Efficient Methods | Netalith
February 28, 2026 - When a file contains very large single lines or binary data, read fixed-size chunks to avoid high peak memory usage. Adjust chunk_size to balance I/O overhead and memory usage. def read_in_chunks(file_object, chunk_size=1024): while True: chunk ...
🌐
Medevel
medevel.com › how-to-read-large-text-files-in-python
How to Read Large Text files in Python?
January 24, 2024 - Rich Ecosystem: Python has a vast ecosystem of libraries and tools for data processing, such as NumPy, Pandas, and NLTK, which can be leveraged along with reading large text files.
🌐
Developer-service
developer-service.blog › handling-large-files-and-optimizing-file-operations-in-python
Handling Large Files and Optimizing File Operations in Python
September 2, 2025 - In addition to reading and writing data efficiently, there are several other optimization techniques you can use to handle large files more effectively. Python’s seek() and tell() functions allow you to navigate through a file without reading the entire content.
🌐
Saturn Cloud
saturncloud.io › blog › reading-large-text-files-with-pandas
Reading Large Text Files with Pandas | Saturn Cloud Blog
May 1, 2026 - Reading large text files with Pandas is a straightforward process that involves a few simple steps. Let’s take a look at how to do it. The first step is to import the Pandas library into your Python script.
Starred by 6 users
Forked by 2 users
Languages: Python 61.8% | C 38.2%
🌐
Oxford Protein Informatics Group
blopig.com › blog › 2016 › 08 › processing-large-files-using-python
Processing large files using python | Oxford Protein Informatics Group
Let’s fix this by reading the data in line by line, so that only a single line is stored in the RAM at any given time. with open("input.txt") as f: for line in f: process(line) This is a big improvement, namely it doesn’t crash when fed a big file (though also it’s shorter!).
🌐
Medium
medium.com › @tubelwj › how-python-reads-large-data-files-of-100gb-in-size-6a4f273c8248
How Python Reads Large Data Files of 100GB in Size? | by Gen. Devin DL. | Medium
February 10, 2025 - One popular technique is memory-mapped files, which allows a file to be mapped directly into memory without loading it entirely, thus improving performance when accessing large files.
🌐
Medium
medium.com › @RobertMcCarter › processing-large-files-with-python-5737b48b7435
Processing large files with Python | by Robert McCarter | Medium
June 25, 2025 - Processing large files with Python Recently, a co worker of mine was trying to load a very large file from an SFTP server into PostgreSQL. Using psycopg2 they were leveraging the standard COPY FROM …
🌐
Rune Hub
rune.codes › home › tutorials › python › read and write large files in python
Python Large Files: Read & Write Without Memory Issues | Rune Hub
July 10, 2026 - There are two streaming patterns: ... are not the natural unit of processing. The for loop iterator pattern is the simplest and most memory-efficient way to process a large text file....