Access file with fileinput
Create fileinput object
The following code adds Line Numbers to a Python Script.
import fileinput# from w w w . java2s . co m
for line in fileinput.input(inplace=True):
line = line.rstrip()
num = fileinput.lineno()
print '%-40s # %2i' % (line, num)