By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,313 Members | 1,145 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,313 IT Pros & Developers. It's quick & easy.

counting lines using fileinput module

P: n/a
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?
Feb 14 '08 #1
Share this Question
Share on Google+
3 Replies


P: n/a
En Wed, 13 Feb 2008 23:47:11 -0200, Robert <ro*********@gmail.com>
escribió:
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

--
Gabriel Genellina

Feb 14 '08 #2

P: n/a
On Feb 13, 6:47*pm, Robert <robert.p...@gmail.comwrote:
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()
Feb 14 '08 #3

P: n/a
On Feb 13, 8:31*pm, 7stud <bbxx789_0...@yahoo.comwrote:
On Feb 13, 6:47*pm, Robert <robert.p...@gmail.comwrote:
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()
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
--------------------------------------------------------
I added count to keep track of the next to last file in the command
line list. The fileinput module opens then closes an empty file. If
there is a cleaner way to do something like this I would interested to
know. Thanks.
Feb 14 '08 #4

This discussion thread is closed

Replies have been disabled for this discussion.