counting lines using fileinput module

R

Robert

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()
 
G

Gabriel Genellina

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):

You forget the () after lineno
 
7

7stud

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()
 
R

Robert

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()

Thanks for the heads up. I hate it when that happens. Anyway, here is
the modified code:
-------------------------------------------------------
#!/usr/bin/python
import fileinput

file = ''
count = 0
# cycle through files
for line in fileinput.input():
if (fileinput.isfirstline()):
if (fileinput.lineno() > 1):
print "%20s has %8d lines" % (file, fileinput.lineno()-1)
file = fileinput.filename()
count = fileinput.lineno()-1 # needed this in case there is an
empty file at the end of list
print "%20s has %8d lines" % (file, fileinput.lineno()-count) # this
is for the last file
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top