Open In App

linecache.getline() in Python

Last Updated : 22 Apr, 2020
Comments
Improve
Suggest changes
Like Article
Like
Report
With the help of linecache.getline() method, we can get a particular line by providing a line number, one of the usecase for this method is to find the syntax error in the given line number within a big file by using linecache.getline() method.
Syntax : linecache.getline(filename, lineno) Return : Return the content of given line number.
Input File : Example #1 : In this example we can see that by using linecache.getline() method, we are able to get the content of a given line from a big file by using this method. Python3 1=1
# import linecache
import linecache as lc

# Using linecache.getline() method
gfg = lc.getline('File.txt', 1)

print(gfg)
Output :   Example #2 : Python3 1=1
# import linecache
import linecache as lc

# Using linecache.getline() method
gfg = lc.getline('File.txt', 2)

print(gfg)
Output :

Next Article
Article Tags :
Practice Tags :

Similar Reads