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 :
<!–
–>

Python: Inplace Editing using FileInput

fileinput.input() in Python

fileinput.filename() in Python

fileinput.lineno() in Python

fileinput.isfirstline() in Python

Important differences between Python 2.x and Python 3.x with examples

Python program to build flashcard using class in Python

Python | Sort Python Dictionaries by Key or Value

Python | Merge Python key values to list

Reading Python File-Like Objects from C | Python




Please Login to comment…