linecache是专门支持读取大文件,而且支持行式读取的函数库。 linecache 预先把文件读入缓存起来,后面如果你访问该文件的话就不再从硬盘读取。对于大文件的读取效率还不错
Help on module linecache: NAME linecache - Cache lines from files. FILE /usr/local/lib/python2.7/linecache.py MODULE DOCS http://docs.python.org/library/linecache DESCRIPTION This is intended to read lines from modules imported -- hence if a filename is not found, it will look down the module search path for a file by that name. FUNCTIONS checkcache(filename=None) Discard cache entries that are out of date. (This is not checked upon each call!) clearcache() Clear the cache entirely. getline(filename, lineno, module_globals=None) DATA __all__ = ['getline', 'clearcache', 'checkcache']