FAQ
hello, I am nwe to Python and I'm trying to write this script but I need it to check how many lines is in the file and I don't know how so I'm hoping someone could tell me.

--
Too often we lose sight of life's simple pleasures. Remember when someone annoys you it takes 42 muscles in your face to frown, BUT, it only takes 4 muscles to extend your arm and bitch-slap that mother@#?!&! upside the head!


-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/python-list/attachments/20020328/19395d91/attachment.htm

Search Discussions

  • Emile van Sebille at Mar 29, 2002 at 2:36 pm
    Joseph Youssef
    hello, I am nwe to Python and I'm trying to write this
    script but I need it to check how many lines is in
    the file and I don't know how so I'm hoping someone
    could tell me.

    len(open('/path/and/filename').readlines())

    --

    Emile van Sebille
    emile at fenx.com

    ---------
  • Phil hunt at Mar 29, 2002 at 6:01 pm

    On Fri, 29 Mar 2002 06:36:14 -0800, Emile van Sebille wrote:
    Joseph Youssef
    hello, I am nwe to Python and I'm trying to write this
    script but I need it to check how many lines is in
    the file and I don't know how so I'm hoping someone
    could tell me.

    len(open('/path/and/filename').readlines())
    Alternatively:

    os.system("wc -l %s" % filename)


    --
    <"><"><"> Philip Hunt <philh at comuno.freeserve.co.uk> <"><"><">
    "I would guess that he really believes whatever is politically
    advantageous for him to believe."
    -- Alison Brooks, referring to Michael
    Portillo, on soc.history.what-if
  • Jeff Shannon at Mar 29, 2002 at 7:39 pm
    In article <slrnaa9b09.i04.philh at comuno.freeserve.co.uk>,
    philh at comuno.freeserve.co.uk says...
    On Fri, 29 Mar 2002 06:36:14 -0800, Emile van Sebille wrote:
    Joseph Youssef
    hello, I am nwe to Python and I'm trying to write this
    script but I need it to check how many lines is in
    the file and I don't know how so I'm hoping someone
    could tell me.
    len(open('/path/and/filename').readlines())
    Alternatively:

    os.system("wc -l %s" % filename)
    Except that this is platform-specific for *nix, whereas
    len(file.readlines()) works on all Python platforms.

    --

    Jeff Shannon
    Technician/Programmer
    Credit International
  • Mark McEahern at Mar 29, 2002 at 2:55 pm
    f = open(filename)
    lines = f.readlines()
    f.close()

    print "%s has %d lines." % (filename, len(lines))

    // mark

    -----Original Message-----
    From: python-list-admin at python.org [mailto:python-list-admin at python.org]On
    Behalf Of Joseph Youssef
    Sent: Thursday, March 28, 2002 9:26 PM
    To: python-list at python.org
    Subject: number of lines in a file


    hello, I am nwe to Python and I'm trying to write this script but I need it
    to check how many lines is in the file and I don't know how so I'm hoping
    someone could tell me.

    --
    Too often we lose sight of life's simple pleasures. Remember when someone
    annoys you it takes 42 muscles in your face to frown, BUT, it only takes 4
    muscles to extend your arm and bitch-slap that mother@#?!&! upside the head!
  • John Machin at Mar 29, 2002 at 9:22 pm
    "Joseph Youssef" <leader730 at hotmail.com> wrote in message news:<b0%o8.22778$Li5.504465 at weber.videotron.net>...
    hello, I am nwe to Python and I'm trying to write this script but I need
    it to check how many lines is in the file and I don't know how so I'm
    hoping someone could tell me.
    Welcome to Python.

    Others have suggested approaches which attempt to read the whole file
    into memory, but may succeed very slowly or not at all in the event
    that you have files that are larger than your machine's physical
    memory; here's a slightly more robust approach:

    num_lines = 0
    f = file('path_and_file_name')
    for line in f:
    num_lines += 1
    f.close()

    HTH,
    John
  • Bengt Richter at Mar 30, 2002 at 1:58 am

    On 29 Mar 2002 13:22:28 -0800, sjmachin at lexicon.net (John Machin) wrote:
    "Joseph Youssef" <leader730 at hotmail.com> wrote in message news:<b0%o8.22778$Li5.504465 at weber.videotron.net>...
    hello, I am nwe to Python and I'm trying to write this script but I need
    it to check how many lines is in the file and I don't know how so I'm
    hoping someone could tell me.
    Welcome to Python.

    Others have suggested approaches which attempt to read the whole file
    into memory, but may succeed very slowly or not at all in the event
    that you have files that are larger than your machine's physical
    memory; here's a slightly more robust approach:

    num_lines = 0
    f = file('path_and_file_name')
    for line in f:
    num_lines += 1
    f.close()
    You can do a one-liner without reading the whole file at once ;-)

    reduce(lambda x,y: x+1, file('/path/and/filename').xreadlines(), 0)

    Regards,
    Bengt Richter

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppython-list @
categoriespython
postedMar 29, '02 at 3:26a
activeMar 30, '02 at 1:58a
posts7
users7
websitepython.org

People

Translate

site design / logo © 2023 Grokbase