Open In App

fileinput.lineno() in Python

Last Updated : 22 Apr, 2020
Comments
Improve
Suggest changes
Like Article
Like
Report
With the help of fileinput.lineno() method, we can get the line number for every line on line read from input file by using fileinput.lineno() method.
Syntax : fileinput.lineno() Return : Return the line number.
Example #1 : In this example we can see that by using fileinput.lineno() method, we are able to get the line number for every line read from the given file by using this method. Input File - Python3 1=1
# import fileinput
import fileinput

# Using fileinput.lineno() method
for line in fileinput.input(files ='gfg.txt'):
    print('{}. '.format(fileinput.lineno()) + line)
Output :   Example #2 : Input File - Python3 1=1
# import fileinput
import fileinput

# Using fileinput.lineno() method
for line in fileinput.input(files =('gfg.txt', 'gfg1.txt')):
    print('{}. '.format(fileinput.lineno()) + line)
Output :

Next Article
Practice Tags :

Similar Reads