Adding Line Numbers to a Python Script : fileinput « Buildin Module « Python Tutorial






import fileinput

for line in fileinput.input(inplace=True):
    line = line.rstrip()
    num = fileinput.lineno()
    print '%-40s # %2i' % (line, num)








14.5.fileinput
14.5.1.Adding Line Numbers to a Python Script