HOWTO · Python
How to Read Specific Lines From a File in Python
Learn how to read specific lines from a file in Python using various methods such as fileobject.readlines(), linecache, and enumerate(). This comprehensive guide covers techniques for small and large files, helping you choose the best approach for your needs. Enhance your file handling skills with practical examples and detailed explanations.
On this page
To read one or more specific lines from a Python text file, first decide whether the line number comes from a person or from Python code. Human line numbers usually start at 1, while list indexes start at 0. For a small file, readlines() loads all lines for direct indexing. For a large file, enumerate() can scan incrementally. For repeated requests, linecache offers line-number access and caches file data, so it is not a constant-memory solution.
Choose a line-numbering convention first
If a person asks for line 3, the corresponding list index is 3 - 1, or 2. enumerate() also starts with index 0 by default. In contrast, linecache.getline(path, 3) expects the human-style line number 3. Keeping these conventions explicit prevents off-by-one mistakes. Text lines often include their trailing newline, so remove it only when the value is being displayed or compared without that whitespace.
Read selected lines from a small file with readlines()
readlines() returns a list containing every line, including its newline when present. This is convenient when the file comfortably fits in memory and you may access several positions. The following self-contained example creates a temporary text file and prints its third line:
from pathlib import Path
from tempfile import TemporaryDirectory
with TemporaryDirectory() as folder:
path = Path(folder) / "notes.txt"
path.write_text("first\nsecond\nthird\nfourth\n", encoding="utf-8")
with path.open(encoding="utf-8") as file:
lines = file.readlines()
print(lines[2].rstrip("\n"))
For a range of human line numbers from first_line through last_line, inclusive, use the slice lines[first_line - 1:last_line]. The slice end is exclusive, which is why the human last-line number itself works as the end index. Check that a requested index is within len(lines) before indexing when the requested line may not exist.
Read selected lines from a large file with enumerate()
Iterating over the file directly avoids building a list of all its lines. enumerate(file) supplies a zero-based index as each line is read; add 1 when displaying a human line number. It still has to pass over earlier lines to reach a later one, so this approach is useful for reducing memory use rather than jumping directly to a distant line.
from pathlib import Path
from tempfile import TemporaryDirectory
with TemporaryDirectory() as folder:
path = Path(folder) / "events.txt"
path.write_text("start\nready\nconnected\nfinished\n", encoding="utf-8")
wanted = {2, 4}
with path.open(encoding="utf-8") as file:
for line_number, line in enumerate(file, start=1):
if line_number in wanted:
print(line_number, line.rstrip("\n"))
if line_number >= max(wanted):
break
Here start=1 makes the loop variable a human line number, which can make selection conditions easier to read. If the file may have fewer lines than requested, the loop simply ends without printing those requests.
Use linecache for repeated line access
Use linecache.getline(path, line_number) when code makes repeated requests by a file’s 1-based line number. It returns the line as text, including its newline when present. A missing line is represented by an empty string. linecache caches file contents for reuse, which may be convenient when the same file is queried repeatedly, but the cached data still uses memory.
import linecache
from pathlib import Path
from tempfile import TemporaryDirectory
with TemporaryDirectory() as folder:
path = Path(folder) / "report.txt"
path.write_text("header\nalpha\nbeta\n", encoding="utf-8")
line = linecache.getline(str(path), 2)
print(line.rstrip("\n"))
linecache.clearcache()
The explicit clearcache() call releases cached entries after this example. For a changing file, cache freshness matters; clear the cache before requesting lines again when your program has changed the file and needs to discard cached data.
Handle ranges, missing lines, and memory limits
Choose readlines() when direct access to a manageable file is more important than holding its contents in memory. Choose enumerate() when processing a large file incrementally or stopping after a selected line; a later target still requires reading preceding lines. Choose linecache for repeated numbered requests when its cache is acceptable. None of these methods can retrieve a line beyond the file’s end: list indexing raises IndexError, iteration produces no match, and getline() returns an empty string. Validate the request or handle that result according to your program’s needs.
Open text files in read mode, preferably with a with statement so the file closes when the block ends. Specify an encoding when the file’s encoding is known. Whole-file loading can use substantial memory as line count and line length grow; there is no universal file-size threshold that fits every machine and workload. Streaming limits the lines retained by the reading loop, while any selected lines you collect will still occupy memory. These tradeoffs make the access pattern and available memory better guides than a fixed size or speed claim.