Get number of lines in file

Discussion in 'Python' started by ssmith579@aol.com, May 27, 2005.

  1. Guest

    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.
     
    , May 27, 2005
    #1
    1. Advertising

  2. On May 27, 2005, at 12:17 PM, 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]
     
    Elliot Temple, May 27, 2005
    #2
    1. Advertising

  3. Guest

    Thanks! I was trying len(cpn_version) and that didn't work.
     
    , May 27, 2005
    #3
  4. Elliot Temple wrote:
    >
    > On May 27, 2005, at 12:17 PM, 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
     
    Steven Bethard, May 28, 2005
    #4
  5. Magnus Lycka Guest

    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\nsdfgsdfgsdfg\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.
     
    Magnus Lycka, May 30, 2005
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jack
    Replies:
    9
    Views:
    2,741
  2. Mullin
    Replies:
    4
    Views:
    5,518
    Harald
    Jun 12, 2005
  3. Joe Wright
    Replies:
    0
    Views:
    555
    Joe Wright
    Jul 27, 2003
  4. Murali
    Replies:
    2
    Views:
    612
    Jerry Coffin
    Mar 9, 2006
  5. kquirici
    Replies:
    13
    Views:
    1,045
    Rainer Weikusat
    Jun 19, 2012
Loading...

Share This Page