# -*- coding: utf-8 -*- #python 27 #xiaodeng #python模块之linecache import linecache ''' >>> help(linecache) Help on module linecache: FUNCTIONS checkcache = extended_linecache_checkcache(filename=None, orig_checkcache=<function checkcache>) Extend linecache.checkcache to preserve the <pyshell#...> entries Rather than repeating the linecache code, patch it to save the <pyshell#...> entries, call the original linecache.checkcache() (skipping them), and then restore the saved entries. orig_checkcache is bound at definition time to the original method, allowing it to be patched. clearcache() Clear the cache entirely. getline(filename, lineno, module_globals=None) DATA __all__ = ['getline', 'clearcache', 'checkcache'] ''' #从名为filename的文件中得到第lineno行。这个函数从不会抛出一个异常–产生错误时它将返回”(换行符将包含在找到的行里)。 #linecache.getline(filename,lineno) #查找特定行(第一行)的数据,返回一个字符串 filename='1.txt' re= linecache.getline(filename,1) print re.strip()#因为本身带有换行符 #得到所有行数据,返回一个list re= linecache.getlines(filename) print re #获取多少行的数据 re= linecache.getlines(filename)[0:4] print re #更新文件名为filename的缓存。如果filename文件更新了,使用这个函数可以更新linecache.getlines(filename)返回的列表 linecache.updatecache(filename) #清理缓存 linecache.clearcache()