How to read text files using LINECACHE in Python

PythonServer Side ProgrammingProgramming

Solution..

The linecache module implements cache which holds the contents of files, parsed into separate lines, in memory. linecache module returns line/s by indexing into a list, and saves time over repeatedly reading the file and parsing lines to find the one desired.

lincecache module is very useful when looking for multiple lines from the same file.

Prepare test data. You can get this text by just using Google and searching for sample text.

Lorem ipsum dolor sit amet, causae apeirian ea his, duo cu congue prodesset. Ut epicuri invenire duo, novum ridens eu has, in natum meliore noluisse sea. Has ei stet explicari. No nam eirmod deterruisset, nusquam electram rationibus ad sea, interesset delicatissimi et sit. Purto molestiae cu eum, in per hinc periculis intellegam.

Id porro facete cum. No est veritus detraxit facilisis, sit ea clita decore essent. Ut eam labores fuisset menandri, ex sit brute viderer eleifend, altera argumentum vel ex. Duo at zril sensibus, eu vim ullum assentior, quando possit at his.

Te nam tempor posidonium scripserit, eam mundi reprimique dissentias ne. Vim te soleat offendit democritum. Nam an diam elaboraret, quaeque dissentias an has. Autem legendos dignissim ad vis, sea ex amet petentium reprehendunt, inermis constituam philosophia ne mel. Esse noster lobortis usu ne.

Nec reque postea urbanitas ut, mea in nulla invidunt ocurreret. Ei duo iuvaret numquam. Ferri nemore audire te est, mel et detracto noluisse. Nec eu habeo justo, id pro posse apeirian volutpat. Mea sonet quaestio ne.

Atqui quaeque alienum te vim. Graeco aliquip liberavisse pro ut. Te similique reformidans usu, te mundi aliquando ius. Meis scripta minimum quo no, meis prima fabellas eu eam, laoreet delicata forensibus ut vim. Et quo vocibus mediocritatem, atqui summo an eam.

Example

import os
import tempfile

text = """
Lorem ipsum dolor sit amet, causae apeirian ea his, duo cu congue prodesset. Ut epicuri invenire duo, novum ridens eu has, in natum meliore noluisse sea. Has ei stet explicari. No nam eirmod deterruisset, nusquam electram rationibus ad sea, interesset delicatissimi et sit. Purto molestiae cu eum, in per hinc periculis intellegam.

Id porro facete cum. No est veritus detraxit facilisis, sit ea clita decore essent. Ut eam labores fuisset menandri, ex sit brute viderer eleifend, altera argumentum vel ex. Duo at zril sensibus, eu vim ullum assentior, quando possit at his.

Te nam tempor posidonium scripserit, eam mundi reprimique dissentias ne. Vim te soleat offendit democritum. Nam an diam elaboraret, quaeque dissentias an has. Autem legendos dignissim ad vis, sea ex amet petentium reprehendunt, inermis constituam philosophia ne mel. Esse noster lobortis usu ne.

Nec reque postea urbanitas ut, mea in nulla invidunt ocurreret. Ei duo iuvaret numquam. Ferri nemore audire te est, mel et detracto noluisse. Nec eu habeo justo, id pro posse apeirian volutpat. Mea sonet quaestio ne.

Atqui quaeque alienum te vim. Graeco aliquip liberavisse pro ut. Te similique reformidans usu, te mundi aliquando ius. Meis scripta minimum quo no, meis prima fabellas eu eam, laoreet delicata forensibus ut vim. Et quo vocibus mediocritatem, atqui summo an eam.

"""

1. Create a Function to create temporary file and delete it after usage.

def make_tempfile():
"""
Function: Create a temporary file.
mkstemp() and mkdtemp() to create temporary files and directories

args: None

return: Temp file name.
"""
fd, temp_file = tempfile.mkstemp()
os.close(fd)

with open(temp_file, 'wt') as f:
f.write(text)

return temp_file

def cleanup(temp_file):
os.unlink(temp_file)

3. Read specific lines using linecache. The line numbers of files read by the linecache module start with 1, unlike lists which start indexing the array from 0. This is an important point to remember.

import os
import tempfile
import linecache

text = """
Lorem ipsum dolor sit amet, causae apeirian ea his, duo cu congue prodesset. Ut epicuri invenire duo, novum ridens eu has, in natum meliore noluisse sea. Has ei stet explicari. No nam eirmod deterruisset, nusquam electram rationibus ad sea, interesset delicatissimi et sit. Purto molestiae cu eum, in per hinc periculis intellegam.

Id porro facete cum. No est veritus detraxit facilisis, sit ea clita decore essent. Ut eam labores fuisset menandri, ex sit brute viderer eleifend, altera argumentum vel ex. Duo at zril sensibus, eu vim ullum assentior, quando possit at his.

Te nam tempor posidonium scripserit, eam mundi reprimique dissentias ne. Vim te soleat offendit democritum. Nam an diam elaboraret, quaeque dissentias an has. Autem legendos dignissim ad vis, sea ex amet petentium reprehendunt, inermis constituam philosophia ne mel. Esse noster lobortis usu ne.

Nec reque postea urbanitas ut, mea in nulla invidunt ocurreret. Ei duo iuvaret numquam. Ferri nemore audire te est, mel et detracto noluisse. Nec eu habeo justo, id pro posse apeirian volutpat. Mea sonet quaestio ne.

Atqui quaeque alienum te vim. Graeco aliquip liberavisse pro ut. Te similique reformidans usu, te mundi aliquando ius. Meis scripta minimum quo no, meis prima fabellas eu eam, laoreet delicata forensibus ut vim. Et quo vocibus mediocritatem, atqui summo an eam.
"""

def make_tempfile():
"""
Function: Create a temporary file.
mkstemp() and mkdtemp() to create temporary files and directories

args: None

return: Temp file name.
"""
directory = os.getcwd()
fd, temp_file = tempfile.mkstemp(dir=directory)
os.close(fd)

with open(temp_file, 'wt') as f:
f.write(text)

return temp_file

def cleanup(temp_file):
os.unlink(temp_file)

# Make a file with ipsum data.
filename = make_tempfile()
print(f"Output \n {filename}")

split_line = '\n'

# Pick the lines from source.

print(f"*** Displaying first 5 lines directly from the source \n {text.split(split_line)[4]}" )

# pick out the same line from cache
print(f" \n *** Displaying first 5 lines from the cache \n {linecache.getline(filename, 5)}" )

# cleanup the tempfile by using unlink
cleanup(filename)

Output

C:\Users\sasan\PycharmProjects\blog\TutorialPoints\Updated_Code\tmpazax_yne
*** Displaying first 5 lines directly from the source
Id porro facete cum. No est veritus detraxit facilisis, sit ea clita decore essent. Ut eam labores fuisset menandri, ex sit brute viderer eleifend, altera argumentum vel ex. Duo at zril sensibus, eu vim ullum assentior, quando possit at his.

*** Displaying first 5 lines from the cache
Id porro facete cum. No est veritus detraxit facilisis, sit ea clita decore essent. Ut eam labores fuisset menandri, ex sit brute viderer eleifend, altera argumentum vel ex. Duo at zril sensibus, eu vim ullum assentior, quando possit at his.

4. Linecache always includes the newline at the end of the line. Therefore, if the line is empty, the return value is just the newline.

See below.

import linecache

# Make a file with ipsum data.
filename = make_tempfile()
print(f"Output \n {filename}")

# Blank lines include the newline.

print(f"\n *** The number of lines in the text is 13." )
print(" \n *** Displaying the lastline from Linecache which should be a new line\n {!r}".format(linecache.getline(filename, 8)) )

cleanup(filename)

Output

C:\Users\sasan\PycharmProjects\blog\TutorialPoints\Updated_Code\tmp352zirvn

*** The number of lines in the text is 13.

*** Displaying the lastline from Linecache which should be a new line
'\n'

5.Conclusion - When an application needs random access to files, linecache makes it easy to read lines by their line number. The contents of the file are maintained in a cache, so be careful of memory consumption.

raja
Published on 09-Nov-2020 10:58:36
Advertisements