counting lines using fileinput module

Robert robert.pena at gmail.com
Wed Feb 13 20:47:11 EST 2008


I would like to count lines in a file using the fileinput module and I
am getting an unusual output.
------------------------------------------------------------------------------
#!/usr/bin/python
import fileinput

# cycle through files
for line in fileinput.input():
   if (fileinput.isfirstline()):
      if (fileinput.lineno > 1):
         print "%8d lines" % (fileinput.lineno()-1)
      print "%s" % fileinput.filename()
print "%8d lines" % fileinput.filelineno()
---------------------------------------------------------------------------------

This works fine except it prints "0 lines" first.
Can anyone help me understand why that is?



More information about the Python-list mailing list