473,692 Members | 2,882 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to count lines in a text file ?

Hi all.

I'm trying to write a program that:
1) Ask me what file I want to count number of lines in, and then counts the
lines and writes the answear out.

2) I made the first part like this:

in_file = raw_input("What is the name of the file you want to open: ")
in_file = open("test.txt" ,"r")
text = in_file.read()

3) I think that I have to use a for loop ( something like: for line in text:
count +=1)
Or maybee I have to do create a def: something like: ( def loop(line,
count)), but not sure how to do this properly.
And then perhaps use the readlines() function, but again not quite sure how
to do this. So do one of you have a good idea.

Thanks for all help

Jul 18 '05 #1
22 61372
Oh I just did it.

Just used the line:

print "%d lines in your choosen file" % len(open("test. txt").readlines ())

Thanks though :)
"Ling Lee" <ja*****@mail.t rillegaarden.dk > wrote in message
news:41******** *************** @nntp02.dk.teli a.net...
Hi all.

I'm trying to write a program that:
1) Ask me what file I want to count number of lines in, and then counts
the lines and writes the answear out.

2) I made the first part like this:

in_file = raw_input("What is the name of the file you want to open: ")
in_file = open("test.txt" ,"r")
text = in_file.read()

3) I think that I have to use a for loop ( something like: for line in
text: count +=1)
Or maybee I have to do create a def: something like: ( def loop(line,
count)), but not sure how to do this properly.
And then perhaps use the readlines() function, but again not quite sure
how to do this. So do one of you have a good idea.

Thanks for all help

Jul 18 '05 #2
Yes, you need a for loop, and a count variable. You can count in several
ways. File objects are iterable, and they iterate over the lines in the
file. readlines() returns a list of the lines, which will have the same
effect, but because it builds the entire list in memory first, it uses
more memory. Example:

########

filename = raw_input('file ? ')
file = open(filename)

lines = 0
for line in file:
# line is ignored here, but it contains each line of the file,
# including the newline
lines += 1

print '%r has %r lines' % (filename, lines)

########

another alternative is to use the standard posix program "wc" with the
-l option, but this isn't Python.

On Mon, Sep 20, 2004 at 03:18:53PM +0200, Ling Lee wrote:
Hi all.

I'm trying to write a program that:
1) Ask me what file I want to count number of lines in, and then counts the
lines and writes the answear out.

2) I made the first part like this:

in_file = raw_input("What is the name of the file you want to open: ")
in_file = open("test.txt" ,"r")
text = in_file.read()

3) I think that I have to use a for loop ( something like: for line in text:
count +=1)
Or maybee I have to do create a def: something like: ( def loop(line,
count)), but not sure how to do this properly.
And then perhaps use the readlines() function, but again not quite sure how
to do this. So do one of you have a good idea.

Thanks for all help

Jul 18 '05 #3
Ling Lee <ja*****@mail.t rillegaarden.dk > wrote:
Oh I just did it.

Just used the line:

print "%d lines in your choosen file" % len(open("test. txt").readlines ())

Thanks though :)


You're welcome;-). However, this approach reads all of the file into
memory at once. If you must be able to deal with humungoug files, too
big to fit in memory at once, try something like:

numlines = 0
for line in open('text.txt' ): numlines += 1
Alex
Jul 18 '05 #4
Ling Lee wrote:
Hi all.

I'm trying to write a program that:
1) Ask me what file I want to count number of lines in, and then counts the
lines and writes the answear out.

2) I made the first part like this:

in_file = raw_input("What is the name of the file you want to open: ")
in_file = open("test.txt" ,"r")
text = in_file.read()

3) I think that I have to use a for loop ( something like: for line in text:
count +=1)
Or maybee I have to do create a def: something like: ( def loop(line,
count)), but not sure how to do this properly.
And then perhaps use the readlines() function, but again not quite sure how
to do this. So do one of you have a good idea.

Thanks for all help

text = in_file.readlin es()
print len(text)

HtH, Roland
Jul 18 '05 #5
Thanks for you replies :)

I just ran the program with a different file name, and it only counts the
number of lines in the file named test.txt. I try to give it a nother try
with your input...

Thanks again... for the fast reply... Hope I get it right this time :)

"Phil Frost" <in****@bitglue .com> wrote in message
news:ma******** *************** *************** @python.org...
Yes, you need a for loop, and a count variable. You can count in several
ways. File objects are iterable, and they iterate over the lines in the
file. readlines() returns a list of the lines, which will have the same
effect, but because it builds the entire list in memory first, it uses
more memory. Example:

########

filename = raw_input('file ? ')
file = open(filename)

lines = 0
for line in file:
# line is ignored here, but it contains each line of the file,
# including the newline
lines += 1

print '%r has %r lines' % (filename, lines)

########

another alternative is to use the standard posix program "wc" with the
-l option, but this isn't Python.

On Mon, Sep 20, 2004 at 03:18:53PM +0200, Ling Lee wrote:
Hi all.

I'm trying to write a program that:
1) Ask me what file I want to count number of lines in, and then counts
the
lines and writes the answear out.

2) I made the first part like this:

in_file = raw_input("What is the name of the file you want to open: ")
in_file = open("test.txt" ,"r")
text = in_file.read()

3) I think that I have to use a for loop ( something like: for line in
text:
count +=1)
Or maybee I have to do create a def: something like: ( def loop(line,
count)), but not sure how to do this properly.
And then perhaps use the readlines() function, but again not quite sure
how
to do this. So do one of you have a good idea.

Thanks for all help

Jul 18 '05 #6
Phil Frost wrote:
another alternative is to use the standard posix program "wc" with the
-l option, but this isn't Python.


Not the same thing. wc -l counts newline bytes, not "real" lines.
Erik
Jul 18 '05 #7
Ling Lee said unto the world upon 2004-09-20 09:36:
Thanks for you replies :)

I just ran the program with a different file name, and it only counts the
number of lines in the file named test.txt. I try to give it a nother try
with your input...

Thanks again... for the fast reply... Hope I get it right this time :)


<SNIP>
On Mon, Sep 20, 2004 at 03:18:53PM +0200, Ling Lee wrote:
Hi all.

I'm trying to write a program that:
1) Ask me what file I want to count number of lines in, and then counts
the
lines and writes the answear out.

2) I made the first part like this:

in_file = raw_input("What is the name of the file you want to open: ")
in_file = open("test.txt" ,"r")
text = in_file.read()

3) I think that I have to use a for loop ( something like: for line in
text:
count +=1)
Or maybee I have to do create a def: something like: ( def loop(line,
count)), but not sure how to do this properly.
And then perhaps use the readlines() function, but again not quite sure
how
to do this. So do one of you have a good idea.

Thanks for all help


Hi Ling Lee,

you've got:

in_file = raw_input("What is the name of the file you want to open: ")
in_file = open("test.txt" ,"r")

What this does is take the user input and assign it the name "in_file"
and then promptly reassigns the name "in_file" to the output of
open("test.txt" ,"r").

So, you never make use of the input, and keep asking it to open test.txt
instead.

Try something like:

in_file_name = raw_input("What is the file you want to open: ")
in_file = open(in_file_na me,"r")

Also, and I say this as a fellow newbie, you might want to check out the
Tutor list: <http://mail.python.org/pipermail/tutor/>

HTH,

Brian vdB

Jul 18 '05 #8
Ling Lee wrote:
2) I made the first part like this:

in_file = raw_input("What is the name of the file you want to open: ")
in_file = open("test.txt" ,"r")
text = in_file.read()
You have two different objects related to the file.
One is the filename (the result of calling raw_input) and
the other is the file handle (the result of calling open).
You are using same variable name for both of them. You
really should make them different.

First you get the file name and reference it by the variable
named 'in_file'. Next you use another filename ("test.txt")
for the open call. This returns a file handle, but not
a file handle to the file named in 'in_file'.

You then change things so that 'in_file' no longer refers
to the filename but now refers to the file handle.

A nicer solution is to use one variable name for the name
(like "in_filenam e") and another for the handle (you can
keep "in_file" if you want to). In the following I
reformatted it so the example fits in under 80 colums

in_filename = raw_input("What is the name of the file "
"you want to open: ")
in_file = open(in_filenam e,"r")
text = in_file.read()
Now the in_file.read() reads all of the file into memory. There
are several ways to count the number of lines. The first is
to count the number of newline characters. Because the newline
character is special, it's most often written as what's called
an escape code. In this case, "\n". Others are backspace ("\b")
and beep ("\g"), and backslash ("\\") since otherwise there's
no way to get the single character "\".

Here's how to cound the number of newlines in the text

num_lines = text.count("\n" )

print "There are", num_lines, "in", in_filename
This will work for almost every file except for one where
the last line doesn't end with a newline. It's rare, but
it does happen. To fix that you need to see if the
text ends with a newline and if it doesn't then add one
more to the count
num_lines = text.count("\n" )
if not text.endswith(" \n"):
num_lines = num_lines + 1

print "There are", num_lines, "in", in_filename

3) I think that I have to use a for loop ( something like
for line in text: count +=1)


Something like that will work. When you say "for xxxx in string"
it loops through every character in the string, and not
every line. What you need is some way to get the lines.

One solution is to use the 'splitlines' method of strings.
This knows how to deal with the "final line doesn't end with
a newline" case and return a list of all the lines. You
can use it like this

count = 0
for line in text.splitlines ():
count = count + 1

or, since splitlines() returns a list of lines you can
also do

count = len(text.splitl ines())

It turns out that reading lines from a file is very common.
When you say "for xxx in file" it loops through every line
in the file. This is not a list so you can't say

len(open(in_fil ename, "r")) # DOES NOT WORK

instead you need to have the explicit loop, like this

count = 0
for line in open(in_filenam e, "r")):
count = count + 1

An advantage to this approach is that it doesn't read
the whole file into memory. That's only a problems
if you have a large file. Try counting the number of
lines in a 1.5 GB file!

By the way, the "r" is the default for the a file open.
Most people omit it from the parameter list and just use

open(in_filenam e)

Hope this helped!

By the way, you might want to look at the "Beginner's
Guide to Python" page at http://python.org/topics/learn/ .
It has pointers to resources that might help, including
the tutor mailing list meant for people like you who
are learning to program in Python.

Andrew
da***@dalkescie ntific.com
Jul 18 '05 #9
Thanks for explaining it that well, really makes sense now :)

Cheers....
"Andrew Dalke" <ad****@mindspr ing.com> wrote in message
news:ek******** ******@newsread 3.news.pas.eart hlink.net...
Ling Lee wrote:
2) I made the first part like this:

in_file = raw_input("What is the name of the file you want to open: ")
in_file = open("test.txt" ,"r")
text = in_file.read()


You have two different objects related to the file.
One is the filename (the result of calling raw_input) and
the other is the file handle (the result of calling open).
You are using same variable name for both of them. You
really should make them different.

First you get the file name and reference it by the variable
named 'in_file'. Next you use another filename ("test.txt")
for the open call. This returns a file handle, but not
a file handle to the file named in 'in_file'.

You then change things so that 'in_file' no longer refers
to the filename but now refers to the file handle.

A nicer solution is to use one variable name for the name
(like "in_filenam e") and another for the handle (you can
keep "in_file" if you want to). In the following I
reformatted it so the example fits in under 80 colums

in_filename = raw_input("What is the name of the file "
"you want to open: ")
in_file = open(in_filenam e,"r")
text = in_file.read()
Now the in_file.read() reads all of the file into memory. There
are several ways to count the number of lines. The first is
to count the number of newline characters. Because the newline
character is special, it's most often written as what's called
an escape code. In this case, "\n". Others are backspace ("\b")
and beep ("\g"), and backslash ("\\") since otherwise there's
no way to get the single character "\".

Here's how to cound the number of newlines in the text

num_lines = text.count("\n" )

print "There are", num_lines, "in", in_filename
This will work for almost every file except for one where
the last line doesn't end with a newline. It's rare, but
it does happen. To fix that you need to see if the
text ends with a newline and if it doesn't then add one
more to the count
num_lines = text.count("\n" )
if not text.endswith(" \n"):
num_lines = num_lines + 1

print "There are", num_lines, "in", in_filename

3) I think that I have to use a for loop ( something like
for line in text: count +=1)


Something like that will work. When you say "for xxxx in string"
it loops through every character in the string, and not
every line. What you need is some way to get the lines.

One solution is to use the 'splitlines' method of strings.
This knows how to deal with the "final line doesn't end with
a newline" case and return a list of all the lines. You
can use it like this

count = 0
for line in text.splitlines ():
count = count + 1

or, since splitlines() returns a list of lines you can
also do

count = len(text.splitl ines())

It turns out that reading lines from a file is very common.
When you say "for xxx in file" it loops through every line
in the file. This is not a list so you can't say

len(open(in_fil ename, "r")) # DOES NOT WORK

instead you need to have the explicit loop, like this

count = 0
for line in open(in_filenam e, "r")):
count = count + 1

An advantage to this approach is that it doesn't read
the whole file into memory. That's only a problems
if you have a large file. Try counting the number of
lines in a 1.5 GB file!

By the way, the "r" is the default for the a file open.
Most people omit it from the parameter list and just use

open(in_filenam e)

Hope this helped!

By the way, you might want to look at the "Beginner's
Guide to Python" page at http://python.org/topics/learn/ .
It has pointers to resources that might help, including
the tutor mailing list meant for people like you who
are learning to program in Python.

Andrew
da***@dalkescie ntific.com

Jul 18 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
3151
by: JD | last post by:
Hi guys I'm trying to write a program that counts the occurrences of HTML tags in a text file. This is what I have so far: #include <stdio.h> #include <stdlib.h> #include <string.h> #define MB 1048576
14
23226
by: mesterak | last post by:
I want to very quickly count the number of lines in text files without having to read each line and increment a counter. I am working in VB.NET and C#. Does anyone have a very fast example on how to do this? Thanks, Matt
68
6803
by: Martin Joergensen | last post by:
Hi, I have some files which has the following content: 0 0 0 0 0 0 0 1 1 1 1 0 0 1 1 1 1 0 0 1 1 1 1 0 0 1 1 1 1 0 0 0 0 0 0 0
3
2184
by: javanooby | last post by:
Hi, I am having problems with this bit of code: public class main { public class readAccounts { reader1 r = new reader1();
8
3739
by: shivam001 | last post by:
I have the following file as the input APPLE 0 118 1 110 1 125 1 135 2 110 3 107 3 115 3 126 ORANGE 0 112 1 119 2 109 2 119 3 112 4 109 4 128 MANGO 0 136 1 143 2 143 3 143 4 136 BANANA 0 5 1 12 1 15 2 13 3 6 3 9 I need to read the above file and have the following information in the output file In APPLE 0 occurs 1 time, 1 occurs 3 times, 2 occurs 1 time, 3 occurs 3 times
3
2707
by: waynejr25 | last post by:
can anyone help me add a function that will count the occurance of each word in an input file. here's the code i have so far it counts the number of characters, words, and lines but i need the occurance of each word. #include <fstream> #include <iostream> #include <string> #include <cstdlib> using namespace std;
16
4432
by: lovecreatesbea... | last post by:
It takes mu so time to finish this C source code line count function. What do you think about it? / ******************************************************************************* * Function : size_t linecnt(char *filenm); * Author : jhlicfoocbar@gmail.com, remove foobar for email * Date : 2008.4.12 * Description: C source code line count. No comments & no space lines
9
13215
by: NvrBst | last post by:
Whats the best way to count the lines? I'm using the following code at the moment: public long GetNumberOfLines(string fileName) { int buffSize = 65536; int streamSize = 65536; long numOfLines = 0; byte bArr = new byte;
3
20441
by: frozz85 | last post by:
I need to search through the file and look for lines that begin with "From". I need to parse the From line and print out the second word for each From line and then also count the number of From lines and print out a count at the end. I'm not sure what to do. Any help is appreciated. import string fname = raw_input("Enter a file name: ") try: infile = open(fname, "r") except: print "File not found:", fname exit()
0
8549
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8815
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,...
0
8812
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7643
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6475
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
5824
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
4332
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...
1
2989
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
2
2245
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.