python linecache读取行更新的实现

模块的作用是:允许从任何文件里得到任何一行或几行,并且使用缓存进行优化。

有几个API接口

linecache.getlines(filename)

从名为filename的文件中得到全部内容,输出为列表格式,以文件每行为列表中的一个元素,并以linenum-1为元素在列表中的位置存储

linecache.getline(filename,lineno)

从名为filename的文件中得到第lineno行。这个函数从不会抛出一个异常–产生错误时它将返回”(换行符将包含在找到的行里)。

如果文件没有找到,这个函数将会在sys.path搜索。

linecache.clearcache()

清除缓存。如果你不再需要先前从getline()中得到的行

linecache.checkcache(filename)

检查缓存的有效性。如果在缓存中的文件在硬盘上发生了变化,并且你需要更新版本,使用这个函数。如果省略filename,将检查缓存里的所有条目。

linecache.updatecache(filename)

更新文件名为filename的缓存。如果filename文件更新了,使用这个函数可以更新linecache.getlines(filename)返回的列表。

用法说明

# 1、获取a.txt文件的内容
>>> a=linecache.getlines('C:/Users/yuan/Desktop/a.txt')
['1a\n', '2b\n', '3c\n', '4d\n', '5e\n', '6f\n', '7g\n']
# 2、获取a.txt文件中第1-4行的内容
>>> a=linecache.getlines('C:/Users/yuan/Desktop/a.txt')[0:4]
>>> a
['1a\n', '2b\n', '3c\n', '4d\n']
# 3、获取a.txt文件中第4行的内容
>>> a=linecache.getline('C:/Users/yuan/Desktop/a.txt',4)
>>>> a
'4d\n'

更新行缓存问题

使用linecache.getlines(filename)或linecache.getline(filename)打开文件的内容之后,如果a.txt文件发生了改变,但是如你再次用linecache.getlines或linecache.getline获取的内容,不是文件的最新内容,还是之前的内容,因为缓存没有更新,此时有两种方法:

1、使用linecache.checkcache(filename)来更新文件在硬盘上的缓存,然后在执行linecache.getlines(‘a.txt’)就可以获取到a.txt的最新内容;

2、直接使用linecache.updatecache(filename),即可获取最新的a.txt的最新内容,但此函数读取返回的是全文。

3、直接每次在linecache.getlines或linecache.getline后使用linecache.clearcache()清理缓存。

另:读取文件之后你不需要使用文件的缓存时需要在最后清理一下缓存,使linecache.clearcache()清理缓存,释放缓存。

这个模块是使用内存来缓存你的文件内容,所以需要耗费内存,打开文件的大小和打开速度和你的内存大小有关系。

import linecache

for i in range(4):  
    linecache.checkcache('C:/Users/yuan/Desktop/cpucheck.txt')         # 更新缓存
    # text = linecache.updatecache('C:/Users/liyuan/Desktop/cpucheck.txt', 4)
    text = linecache.getline('C:/Users/yuan/Desktop/cpucheck.txt', 3)      # 读取第三行

    print(text)
linecache.clearcache()    # 清空、释放缓存

原文地址:https://blog.csdn.net/RyanLee90/article/details/129726437