4.5 Reading a Particular Line from a File


Credit: Luther Blissett

4.5.1 Problem

You want to extract a single line from a file.

4.5.2 Solution

The standard linecache module makes this a snap:

import linecache theline = linecache.getline(thefilepath, desired_line_number)

4.5.3 Discussion

The standard linecache module is usually the optimal Python solution for this task, particularly if you have to do this repeatedly for several of a file's lines, as it caches the information it needs to avoid uselessly repeating work. Just remember to use the module's clearcache function to free up the memory used for the cache, if you won't be getting any more lines from the cache for a while but your program keeps running. You can also use checkcache if the file may have changed on disk and you require the updated version.

linecache reads and caches all of the text file whose name you pass to it, so if it's a very large file and you need only one of its lines, linecache may be doing more work than is strictly necessary. Should this happen to be a bottleneck for your program, you may get some speed-up by coding an explicit loop, encapsulated within a function. Here's how to do this in Python 2.2:

def getline(thefilepath, desired_line_number):     if desired_line_number < 1: return ''     current_line_number = 0     for line in open(thefilepath):         current_line_number += 1         if current_line_number == desired_line_number: return line     return ''

It's not much worse in Python 2.1 you just need to change the for statement into this slightly slower and less concise form:

for line in open(thefilepath).xreadlines(  ):

Python 2.0 and earlier had no such facilities for speedy reading of huge text files, line by line, consuming bounded amounts of memory. Should you be stuck with one of these older versions of Python, linecache will probably be the preferable solution under most circumstances.

4.5.4 See Also

Documentation for the linecache module in the Library Reference; Perl Cookbook Recipe 8.8.



Python Cookbook
Python Cookbook
ISBN: 0596007973
EAN: 2147483647
Year: 2005
Pages: 346

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net