Python-linecache Module Read File usage

Source: Internet
Author: User

Python-linecache module Read File usage detailed


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.


1. 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


2, 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.


3, Linecache.clearcache ()

Clears the cache. If you no longer need a previously obtained row from Getline ()


4. 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.


5. 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.


Self-test 450M file Execution time, or open () method faster, larger than 1G files, using the Linecache.getlines () method better, the code is as follows:

# # #使用linecache. Getlines Method # # #

$cat read_file.py

#!/usr/bin/env python
#coding: UTF8
Import Os,linecache,time
def get_content (file):
Content= "
Cache_data=linecache.getlines (file)
For line in range (len (cache_data)):
Content+=cache_data[line]

return content

def main ():
File= '/opt/file_operate/ibdata1 '
Content=get_content (file)

Print content

Linecache.clearcache ()
If __name__== ' __main__ ':
Starttime=time.clock ()
Main ()
Endtime=time.clock ()
Print "Total time:%s"% (Endtime-starttime)

# # #使用open () method # # #

$cat read_file1.py

#!/usr/bin/env python
#coding: UTF8
Import time
def read_file (file):
Fo=open (file, ' R ')
F=open ('/opt/file_operate/c.txt ', ' W ')
For line in Fo.readlines ():
F.write (line)
Fo.close ()
F.close ()
If __name__== ' __main__ ':
Starttime=time.clock ()
Read_file ('/opt/file_operate/ibdata1 ')
Endtime=time.clock ()
Print "Total time:%s"% (Endtime-starttime)


Note: Learn about the contents of the blog part of Viagra

This article is from the "Snail's Home" blog, be sure to keep this source http://winters.blog.51cto.com/5617866/1609307

Python-linecache Module Read File usage

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.