python 读取文件指定某行
1
2
3
4
5
#encoding=utf-8
#从文件中读取某一行 linecache.checkcache可以刷新cache ,linecache可以缓存某一行的信息
import linecache
line = linecache.getline(r'1.txt', 2)
print line
#如果文件比较大 使用下面
1
2
3
4
5
6
def getline(thefilepath,line_num):
if line_num < 1 :return ''
for currline,line in enumerate(open(thefilepath,'rU')):
if currline == line_num -1 : return line
return ''
lines22=getline('1.txt',2)
页:
[1]