The function of the module is to allow any line or lines to be obtained from any file and use caching for optimization.
linecache.getlines(filename)
Get all the contents from the file named filename, and output it in list format, in file Each line is an element in the list, and is stored with linenum-1 as the position of the element in the list
linecache.getline(filename,lineno)
From the file named filename Get the lineno line. This function will never throw an exception - on an error it will return "" (newline characters will be included in the found line).
If the file is not found, this function will search sys.path.
linecache.clearcache()
Clear the cache. If you no longer need the line previously obtained from getline()
linecache.checkcache(filename)
Check the validity of the cache. If a file in the cache has changed on the hard disk and you need to update the version, use this function. If filename is omitted, all entries in the cache will be checked.
linecache.updatecache(filename)
Update the cache with the filename filename. If the filename file is updated, use this function to update the list returned by linecache.getlines(filename).
Usage instructions
# 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'
After using linecache.getlines(filename) or linecache.getline(filename) to open the content of the file, if the a.txt file changes , but if you use linecache.getlines or linecache.getline again, the content obtained is not the latest content of the file, but the previous content, because the cache has not been updated. There are two methods at this time:
1. Use linecache .checkcache(filename) to update the file cache on the hard disk, and then execute linecache.getlines(‘a.txt’) to get the latest content of a.txt;
2. Use linecache directly .updatecache(filename), you can get the latest content of the latest a.txt, but this function reads and returns the full text.
3. Use linecache directly after linecache.getlines or linecache.getline. .clearcache() clears the cache.
Another: After reading the file, if you do not need to use the file cache, you need to clear the cache at the end, so that linecache.clearcache() clears the cache and releases the cache.
This module uses memory to cache your file content, so it requires memory. The size and opening speed of the opened file are related to your memory size.
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() # 清空、释放缓存
The above is the detailed content of How to implement python linecache read line update. For more information, please follow other related articles on the PHP Chinese website!