Yi,
Use the linecache module.
Yi, *don't* use the linecache module without carefully comparing the
documentation and the implementation with your requirements.
You will find that you have the source code on your computer -- mine
(Windows box) is at c:\Python24\Lib\linecache.py. When you read right
down to the end (it's not a large file, only 108 lines), you'll find this:
try:
fp = open(fullname, 'rU')
lines = fp.readlines()
fp.close()
except IOError, msg:
## print '*** Cannot open', fullname, ':', msg
return []
size, mtime = stat.st_size, stat.st_mtime
cache[filename] = size, mtime, lines, fullname
Looks like it's caching the *whole* of *each* file. Not unreasonable
given it appears to have been written to get source lines to include in
tracebacks.
It might just not be what you want if as you say you have "a huge txt
file". How many megabytes is "huge"?
Cheers,
John
The documentation states that :