Python linecache.getline() Function

Python linecache Module:

The linecache module allows you to read any line from a Python source file while attempting to optimise internally using a cache, which is useful in the case where numerous lines are read from a single file. The traceback module uses this to retrieve source lines for inclusion in the formatted traceback.

Python linecache.getline() Function:

We can obtain a specific line by providing a line number using the linecache.getline() method. One use case for this method is to locate/find the syntax error in the specified line number within a large file by using the linecache.getline() function.

Syntax:

linecache.getline(file_name, line_number)

Return Value:

The content(data) of the given text file is returned by the getline() fnction.

Let us take the text file samplefile.txt as an example here.

samplefile.txt:

Hello this is Python-programs
Good morning all
Welcome to the Python coding platform
Hurry up!!!!

linecache.getline() Function in Python

Method #1: Using getline() Function (Static Input)

Approach:

  • Import linecache module using the import keyword
  • Pass some random text file name, line number as arguments to the getline() function of the linecache module to get the content of the given line number.
  • Store it in a variable.
  • Print the content of the given line.
  • The Exit of the Program.

Below is the implementation:

# Import linecache module using the import keyword
import linecache 

# Pass some random text file name, line number as arguments to the 
# getline() function of the linecache module to get the content of the 
# given line number.
# Store it in a variable.
rslt = linecache.getline('samplefile.txt', 2)

# Print the content of the given line.
print("The content of the given line-2 is:")
print(rslt)

Output:

The content of the given line-2 is:
Good morning all

Method #2: Using getline() Function (User Input)

Approach:

  • Import linecache module using the import keyword.
  • Give the line number as user input using the int(input()) function and store it in a variable.
  • Pass some random text file name, above given line number as arguments to the getline() function of the linecache module to get the content of the given line number.
  • Store it in a variable.
  • Print the content of the given line.
  • The Exit of the Program.

Below is the implementation:

# Import linecache module using the import keyword
import linecache 

# Give the line number as user input using the int(input()) function 
# and store it in a variable.
gvn_line = int(input("Enter some random number = "))

# Pass some random text file name, above given line number as arguments to the 
# getline() function of the linecache module to get the content of the 
# given line number.
# Store it in a variable.
rslt = linecache.getline('samplefile.txt', gvn_line)

# Print the content of the given line.
print("The content of the given line {",gvn_line,"} is:")
print(rslt)

Output:

Enter some random number = 1
The content of the given line { 1 } is:
Hello this is Python-programs