473,748 Members | 2,214 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

counting lines using fileinput module

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.isfi rstline()):
if (fileinput.line no 1):
print "%8d lines" % (fileinput.line no()-1)
print "%s" % fileinput.filen ame()
print "%8d lines" % fileinput.filel ineno()
---------------------------------------------------------------------------------

This works fine except it prints "0 lines" first.
Can anyone help me understand why that is?
Feb 14 '08 #1
3 3593
En Wed, 13 Feb 2008 23:47:11 -0200, Robert <ro*********@gm ail.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.isfi rstline()):
if (fileinput.line no 1):
You forget the () after lineno

--
Gabriel Genellina

Feb 14 '08 #2
On Feb 13, 6:47*pm, Robert <robert.p...@gm ail.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.isfi rstline()):
* * * if (fileinput.line no 1):
* * * * *print "%8d lines" % (fileinput.line no()-1)
* * * print "%s" % fileinput.filen ame()
print "%8d lines" % fileinput.filel ineno()
--------------------------------------------------------------------------- ------

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.linen o v. fileinput.linen o()

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.isfi rstline()):
print fileinput.linen o #<-------*****
if (fileinput.line no 1):
print "%8d lines" % (fileinput.line no()-1)
print "%s" % fileinput.filen ame()
print "%8d lines" % fileinput.filel ineno()
Feb 14 '08 #3
On Feb 13, 8:31*pm, 7stud <bbxx789_0...@y ahoo.comwrote:
On Feb 13, 6:47*pm, Robert <robert.p...@gm ail.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.isfi rstline()):
* * * if (fileinput.line no 1):
* * * * *print "%8d lines" % (fileinput.line no()-1)
* * * print "%s" % fileinput.filen ame()
print "%8d lines" % fileinput.filel ineno()
--------------------------------------------------------------------------- ------
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.linen o *v. fileinput.linen o()

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.isfi rstline()):
* * * * print fileinput.linen o *#<-------*****
* * * * if (fileinput.line no 1):
* * * * * * *print "%8d lines" % (fileinput.line no()-1)
* * * * print "%s" % fileinput.filen ame()
print "%8d lines" % fileinput.filel ineno()
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.isfi rstline()):
if (fileinput.line no() 1):
print "%20s has %8d lines" % (file, fileinput.linen o()-1)
file = fileinput.filen ame()
count = fileinput.linen o()-1 # needed this in case there is an
empty file at the end of list
print "%20s has %8d lines" % (file, fileinput.linen o()-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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
1846
by: Chris Connett | last post by:
I just started working with the fileinput module, and reading the bit about inplace modification, it seems very dangerous: From the doc: --- Optional in-place filtering: if the keyword argument inplace=1 is passed to input() or to the FileInput constructor, the file is moved to a backup file and standard output is directed to the input file **(if a file of the same name as the backup file already exists, it
6
2517
by: calmar | last post by:
Hi all, I would like to use python for a replacement for some binutils. I would like to be able to pipe things into python. Actually I would not like writing a 'script' to handle the input, but write the python commands right onto the command line. It would be possible to use here-documents (for having the correct identation during e.g while loops) for writing python code on the fly, but then how and where to get the pipe output:
4
2900
by: Ben Rf | last post by:
Hi I'm new to programming and i'd like to write a program that will parse a list produced by md5summer and give me a report in a text file on which md5 sums appear more than once and where they are located. the end end goal is to have a way of finding duplicate files that are scattered across a lan of 4 windows computers. I've dabbled with different languages over the years and i think
1
6927
by: j | last post by:
Hi, I've been trying to do line/character counts on documents that are being uploaded. As well as the "counting" I also have to remove certain sections from the file. So, firstly I was working with uploaded MS WORD .doc files. Using code like that below: strLine = sr.ReadLine While Not IsNothing(strLine) 'Not eof If Trim(strLine) <> "" Then 'Not blank
5
2486
by: the.theorist | last post by:
I was writing a small script the other day with the following CLI prog * I've used getopt to parse out the possible options, so we'll ignore that part, and assume for the rest of the discussion that args is a list of file names (if any provided). I used this bit of code to detect wether i want stdinput or not. if len(args)==0:
0
1569
by: Phoe6 | last post by:
Hi All, I am able to use urlib2 through proxy. I give proxy credentials and use # Set the Proxy Address proxy_ip = "10.0.1.1:80" proxy_user = 'senthil_or' proxy_password_orig='password'
11
1494
by: Andy | last post by:
Hi, the file below will print all the keywords in a file and also the line # of the keyword. What I couldn't figure out is to count those keywords per line. For example - "Line #1 has 3 keywords" Can I do like - total = total + numwords(k) "Line number %d has %d keywords" % (j, total) Seems sort of "illegal" in Python?
0
882
by: Fredrik Lundh | last post by:
Francesco Pietra wrote: you may find the fileinput module helpful, combined with the tips you've already gotten in this thread. reference: http://docs.python.org/lib/module-fileinput.html
2
4099
by: rka77 | last post by:
Hi, I am trying to make a Python2.6 script on a Win32 that will read all the text files stored in a directory and print only the lines containing actual data. A sample file - Set : 1 Date: 10212009 12 34 56 25 67 90 End Set ******** Set: 2 Date: 10222009
0
8991
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9552
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9376
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9326
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6796
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6076
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4607
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4877
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3315
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.