Python linecache module Read file usage detailed

Source: Internet
Author: User

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

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.