469,903 Members | 1,664 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,903 developers. It's quick & easy.

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 48724

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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by deko | last post: by
11 posts views Thread by Jeff Wagner | last post: by
2 posts views Thread by Jesse Noller | last post: by
18 posts views Thread by Vasilis Serghi | last post: by
2 posts views Thread by Kururu | last post: by
1 post views Thread by payalp | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.