Python development _linecache

#From thelinecacheThe name, we can know the module and cache (CACHE) about
#linecacheThe document read into the cache, when after the access to the file, it is not necessary to reading from disk
#I often read for those with very high frequency file

You can refer to: open()

Effect of operation:

================================================

Part of the code:

================================================

 1 #python linecache
 2 
 3 #From the name of the linecache, we can know the module and cache (CACHE) about
 4 #Linecache the file read into the cache, when after the access to the file, it is not necessary to reading from disk
 5 #I often read for those with very high frequency file
 6 
 7 import os
 8 import linecache
 9 
10 def get_content(path):
11     '''Read the cache file contents, and return a string representation'''
12     if os.path.exists(path):
13         content = ''
14         cache_data = linecache.getlines(path)
15         for line in range(len(cache_data)):
16             content += cache_data[line]
17         return content
18     else:
19         print('the path [{}] is not exist!'.format(path))
20 
21 def main():
22     path = 'c:\\test.txt'
23     content = get_content(path)
24     print(content)
25 
26 if __name__ == '__main__':
27     main()

Posted by Ulysses at November 24, 2013 - 6:23 AM