Wednesday, July 3, 2024
HomeLanguagesPythonfileinput.lineno() in Python

fileinput.lineno() in Python

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 –




# 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 –




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


Output :

Last Updated :
22 Apr, 2020
Like Article
Save Article

<!–

–>

Similar Reads
Related Tutorials
Calisto Chipfumbu
Calisto Chipfumbuhttp://cchipfumbu@gmail.com
I have 5 years' worth of experience in the IT industry, primarily focused on Linux and Database administration. In those years, apart from learning significant technical knowledge, I also became comfortable working in a professional team and adapting to my environment, as I switched through 3 roles in that time.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments