How to read specific lines from a file by line number?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Reading specific lines from a file is a common need in scripts, log processors, and simple data tools. The right technique depends on whether you need one line or many, whether the file is large, and whether the requested line numbers are sorted.
The Simple And Reliable Approach
For ordinary text files, iterate through the file with enumerate and collect the lines you care about.
This is easy to understand and works well for a moderate number of line requests.
Why This Is Usually Good Enough
Text files do not naturally support constant-time random access by line number, because line lengths vary. To reach line 5000, a program usually has to scan the earlier lines unless it already built an index.
That is why a straightforward sequential scan is often the correct baseline instead of a sign that the code is naive.
Stop Early If The Requests Are Sorted
If the requested line numbers are known in ascending order, you can stop reading once you pass the largest requested line.
That small optimization matters when the file is huge and the requested lines are near the top.
Reading A Single Known Line
If you need just one line, a generator expression can keep the code compact.
This still scans from the top, but it stops as soon as the target line is reached.
linecache Can Be Useful For Repeated Access
Python also provides linecache, which caches file contents by line number.
This can be convenient for repeated lookups in the same file, but it is easy to misuse if the file changes while your process is running, because cached results may become stale.
Large Files And Repeated Queries
If you repeatedly query many arbitrary line numbers in a very large file, the real optimization is to build an index of byte offsets. That is more advanced, but the idea is simple: scan the file once, record where each line starts, and then seek directly to the offsets later.
That approach is worth the complexity only when repeated random access genuinely matters.
Encoding And Newlines Still Matter
Always open the file with the correct encoding when line content matters. If you use text mode, Python handles newline decoding for you, and rstrip("\n") is often enough for clean output. If the file can contain Windows line endings, rstrip() or rstrip("\r\n") may be a better cleanup choice.
Common Pitfalls
The most common mistake is assuming text files allow direct random access by line number the same way arrays do. Another is forgetting that line numbering is usually one-based in user-facing contexts even though many APIs are zero-based elsewhere. Developers also sometimes use linecache on files that change underneath them and then wonder why the returned content is stale. Finally, for huge files, it helps to stop scanning once the highest requested line has been read.
Summary
- Sequential scanning with
enumerateis the standard reliable way to read specific text-file lines. - Stop early when the highest requested line number is known.
- '
linecacheis convenient for repeated lookups but can become stale if the file changes.' - Text files do not naturally provide constant-time random access by line number.
- Build an index only when repeated large-file random access justifies the extra complexity.

