counting lines using fileinput module
7stud
bbxx789_05ss at yahoo.com
Wed Feb 13 21:31:42 EST 2008
On Feb 13, 6:47 pm, Robert <robert.p... at gmail.com> wrote:
> 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?
if '<function lineno at 0x57e70>' > 1:
print 'yes'
--output:--
yes
fileinput.lineno v. fileinput.lineno()
Whenever you have strange problems like that, insert a bunch of print
statements to verify that the values are what you think they should
be:
for line in fileinput.input():
print line #<-------*****
if (fileinput.isfirstline()):
print fileinput.lineno #<-------*****
if (fileinput.lineno > 1):
print "%8d lines" % (fileinput.lineno()-1)
print "%s" % fileinput.filename()
print "%8d lines" % fileinput.filelineno()
More information about the Python-list
mailing list