473,395 Members | 1,629 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Get number of lines in file

I have read in a file and need to get the number of lines.

cpn_file = open('Central Part number list.txt')
cpn_version = cpn_file.read().split('\n')

I want to know the number of elements in cpn_version.

Jul 19 '05 #1
4 48942

On May 27, 2005, at 12:17 PM, ss*******@aol.com wrote:
I have read in a file and need to get the number of lines.

cpn_file = open('Central Part number list.txt')
cpn_version = cpn_file.read().split('\n')

I want to know the number of elements in cpn_version.


Could you use:

count_lines = len(cpn_file.readlines())

-- Elliot Temple
http://www.curi.us/
---
[This E-mail scanned for viruses by Declude Virus]

Jul 19 '05 #2
Thanks! I was trying len(cpn_version) and that didn't work.

Jul 19 '05 #3
Elliot Temple wrote:

On May 27, 2005, at 12:17 PM, ss*******@aol.com wrote:
I have read in a file and need to get the number of lines.

cpn_file = open('Central Part number list.txt')
cpn_version = cpn_file.read().split('\n')

I want to know the number of elements in cpn_version.


Could you use:

count_lines = len(cpn_file.readlines())


Or if you're worried about reading all of cpn_file into memory at once,
you could try something like:

sum(1 for line in cpn_file)

or in Python 2.3:

sum([1 for line in cpn_file])

STeVe
Jul 19 '05 #4
ss*******@aol.com wrote:
Thanks! I was trying len(cpn_version) and that didn't work.


What's your problem? You get a value that's one more than
you expected? You should use splitlines() instead of split('\n'),
or easier, use readlines() instead of read(). Of course, with
a modern python you can just iterate over the file, but note
the difference between split and splitlines when the last line
is complete and ends with a newline character:
a = """dgdgsdfg .... sdfgsdfgsdfg
.... sdfgsdfgsdfg
.... sdfgsdfgsdfg
.... """ a 'dgdgsdfg\nsdfgsdfgsdfg\nsdfgsdfgsdfg\nsdfgsdfgsdf g\n' a.split('\n') ['dgdgsdfg', 'sdfgsdfgsdfg', 'sdfgsdfgsdfg', 'sdfgsdfgsdfg', ''] a.splitlines() ['dgdgsdfg', 'sdfgsdfgsdfg', 'sdfgsdfgsdfg', 'sdfgsdfgsdfg']
If you're allergic to splitlines ;) you could do...
a.rstrip().split('\n') ['dgdgsdfg', 'sdfgsdfgsdfg', 'sdfgsdfgsdfg', 'sdfgsdfgsdfg']

....but it depends how you want to view files that end with
several linefeeds in a row (or other whitespace for that
matter).
a = """"dfgdfg .... dfgdfg
....
.... dgfdfg
....
....
....
.... """ a.split('\n') ['"dfgdfg', 'dfgdfg', '', 'dgfdfg', '', '', '', ''] a.splitlines() ['"dfgdfg', 'dfgdfg', '', 'dgfdfg', '', '', ''] a.rstrip().split('\n')

['"dfgdfg', 'dfgdfg', '', 'dgfdfg']

In other words, the right solution depends on what behaviour
you want for such cases (if they might exist with your files).

Experimenting like this with the interpreter is a very
convenient way to get a grip on things in Python, and one of
the reasons that Python debugging is usually quicker than
debugging in other languages.
Jul 19 '05 #5

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

Similar topics

5
by: deko | last post by:
I have a text file ("eighty.txt") that looks like this: 83|84|85|86 I can read the file into an array like this: $numbers= file("eighty.txt"); But how do I key the array? I'd like to use...
11
by: Jeff Wagner | last post by:
I am importing a file which contains a persons name (firstName, middleName, etc). If I define a function to do this, how can I use the variables outside of that function? Here is the code: ...
2
by: Jesse Noller | last post by:
I am a relative newbie to python and I am having issues trying to iterate over the lines of a file. I have a text file - foo.bar inside of this file are lines of text: x-3411342 y-1324123...
7
by: Melissa | last post by:
I'm trying to create a function that I can put in a query field that will consecutively number the records returned by the query starting at 1 and will start at 1 each time the query is run. So far...
18
by: Vasilis Serghi | last post by:
Presently I define the number of lines to be expected in a file when defining the array size and the initialisation of this array. This works fine for now, but i'm sure that in the future this...
14
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...
2
by: Kururu | last post by:
Hi Does anyone know how to make a form application which can get no. of file in the directory and size??? Thousand Thanks Kururu
2
by: mtchampi | last post by:
I am having difficulty creating a report in MS Access that will subtotal the number of files per created id. Have query that will give grand total, but I need the subtotals first and not sure how. ...
1
by: payalp | last post by:
(1) Reads from the file “hw4.txt” in the local directory the SID (which is a string) and the scores (which are integers) of each student in a class. Although the number of students and the number of...
1
by: ranaharis | last post by:
how can i get line number of file where error has occured?
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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,...
0
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...
0
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...

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.