The Linecache module allows any row to be fetched from any file, and is optimized with caching, as is often the case with multiple lines being read from a single file.
Linecache.getlines (filename)
Get all the content from the file named filename, output it as a list format, one element in each behavior list for the file, and store it as an element in the list in linenum-1
Linecache.getline (Filename,lineno)
Get the line Lineno from the file named filename. This function never throws an exception-it will return when an error occurs (the newline will be included in the found row).
If the file is not found, the function will be searched in Sys.path.
Linecache.clearcache ()
Clears the cache. If you no longer need a previously obtained row from Getline ()
Linecache.checkcache (filename)
Check the validity of the cache. If the files in the cache have changed on the hard disk, and you need to update the version, use this function. If you omit filename, all entries in the cache are checked.
Linecache.updatecache (filename)
Update the cache with file name filename. If the filename file is updated, use this function to update the list returned by linecache.getlines (filename).
Examples of usage:
# Cat A.txt
1a
2b
3c
4d
5e
6f
7g
1. Get the contents of the A.txt file
>>> a=linecache.getlines (' a.txt ')
>>> A
[' 1a\n ', ' 2b\n ', ' 3c\n ', ' 4d\n ', ' 5e\n ', ' 6f\n ', ' 7g\n ']
2. Get the contents of the 第1-4 line in the A.txt file
>>> a=linecache.getlines (' a.txt ') [0:4]
>>> A
[' 1a\n ', ' 2b\n ', ' 3c\n ', ' 4d\n ']
3. Get the contents of line 4th in the A.txt file
>>> a=linecache.getline (' A.txt ', 4)
>>> A
' 4d\n '
Note: After you use Linecache.getlines (' a.txt ') to open the contents of the file, If the a.txt file changes, such as what you get again with Linecache.getlines, not the newest content of the file, or the previous content, there are two ways to do this:
1, use Linecache.checkcache (filename) to update the file on the hard disk cache, and then execute Linecache.getlines (' a.txt ') to obtain the latest content of a.txt;
2, directly use Linecache.updatecache (' a.txt '), you can get the latest a.txt of the latest content
Another: After reading the file you do not need to use the file cache when you need to clean up the cache at the end, so that Linecache.clearcache () clean up the cache, release the cache.
This module uses memory to cache the contents of your files, so you need to consume memory, open file size and open speed and your memory size has a relationship.
Python linecache module Read file usage detailed