Friday, October 24, 2025
HomeLanguagesfileinput.filelineno() in Python

fileinput.filelineno() in Python

With the help of fileinput.filelineno() method, we can get the line number for every line on line read for every file from input file by using fileinput.filelineno() method.

Syntax : fileinput.filelineno()

Return : Return the line number for every file.

Example #1 :
In this example we can see that by using fileinput.filelineno() method, we are able to get the line number for every line read for every file from the given files by using this method.

Input File –




# import fileinput
import fileinput
  
# Using fileinput.filelineno() method
for line in fileinput.input(files ='gfg.txt'):
    print('{}. '.format(fileinput.filelineno()) + line)


Output :

 
Example #2 :

Input File –




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


Output :

Last Updated :
22 Apr, 2020
Like Article
Save Article

<!–

–>

Similar Reads
Related Tutorials
Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11954 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS