import linecache
poem =
'''\
Programming is fun
When the work is done
if you wanna make your work also fun:
use Python!
'''
f = file('poem.txt', 'w')
f.write(poem)
f.close()
file_t = linecache.getline('poem.txt', 6)
print file_t
一、linecache模块简介
我们看一下这个模块的名字叫做linecache,行-缓存,这对于读取内容非常多的文件,效果甚好,而且它还可以读取指定的行内容。
二、linecache模块函数讲解
1 )、 linecache.getline(filename, lineno[, module_globals]) ,这个方法从filename也就是文件中读取内容,得到第 lineno行,注意没有去掉换行符,它将包含在行内。
2 )、 linecache.clearcache() ,清除现有的文件缓存。
3 )、 linecache.checkcache([filename]) ,参数是文件名,作用是检查缓存内容的有效性,可能硬盘内容发生了变化,更新了,如果不提供参数,将检查缓存中所有的项。
三 、linecache模块注意事项
linecache里面最常用到的就是getline方法,简单实用可以直接从内容中读到指定的行,日常编程中如果涉及读取大文件,一定要使用首选linecache模块,相比open()那种方法要快N倍,它是你读取文件的效率之源