On Feb 13, 6:47 pm, Robert <[EMAIL PROTECTED]> 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()
-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to