How do I tell the difference between the end of a text file, and an empty line in a text file?

Discussion in 'Python' started by walterbyrd, May 16, 2007.

  1. walterbyrd

    walterbyrd Guest

    Python's lack of an EOF character is giving me a hard time.

    I've tried:

    -----
    s = f.readline()
    while s:
    ..
    ..
    s = f.readline()
    --------

    and

    -------
    s = f.readline()
    while s != ''
    ..
    ..
    s = f.readline()
    -------


    In both cases, the loop ends as soon it encounters an empty line in
    the file, i.e.


    xxxxxxxxxx
    xxxxxxxxxxx
    xxxxxxx
    < - - - loop end here
    xxxxxxxxxxxxxx
    xxxxxxxxxx
    x
    < ---- loop should end here
     
    walterbyrd, May 16, 2007
    #1
    1. Advertising

  2. walterbyrd

    James Stroud Guest

    Re: How do I tell the difference between the end of a text file,and an empty line in a text file?

    walterbyrd wrote:
    > Python's lack of an EOF character is giving me a hard time.
    >
    > I've tried:
    >

    [ stuff ]

    for s in f:
    do_whatever_with_s(s)


    James
     
    James Stroud, May 16, 2007
    #2
    1. Advertising

  3. On 2007-05-16, walterbyrd <> wrote:

    > Python's lack of an EOF character is giving me a hard time.


    No it isn't.

    > s = f.readline()
    > while s:
    > .
    > .
    > s = f.readline()




    > s = f.readline()
    > while s != ''
    > .
    > .
    > s = f.readline()



    Neither one of your examples is legal Python. Please post real
    code.

    > In both cases, the loop ends as soon it encounters an empty line in
    > the file, i.e.


    No, it doesn't. Not if you've done something reasonable like
    this:

    f = open('testdata','r')
    while True:
    s = f.readline()
    if not s: break
    print repr(s)

    or this:

    f = open('testdata','r')
    s = f.readline()
    while s:
    print repr(s)
    s = f.readline()

    Please post real, runnable code. You've done something wrong
    and we've no way to guess what it was if you won't show us your
    code.

    --
    Grant Edwards grante Yow! Is something VIOLENT
    at going to happen to a
    visi.com GARBAGE CAN?
     
    Grant Edwards, May 16, 2007
    #3
  4. walterbyrd

    James Stroud Guest

    Re: How do I tell the difference between the end of a text file,and an empty line in a text file?

    Grant Edwards wrote:
    > On 2007-05-16, walterbyrd <> wrote:
    >
    >
    >>Python's lack of an EOF character is giving me a hard time.

    >
    >
    > No it isn't.
    >
    >
    >>s = f.readline()
    >>while s:
    >>.
    >>.
    >>s = f.readline()

    >
    >
    >
    >
    >>s = f.readline()
    >>while s != ''
    >>.
    >>.
    >>s = f.readline()

    >
    >
    >
    > Neither one of your examples is legal Python. Please post real
    > code.
    >
    >
    >>In both cases, the loop ends as soon it encounters an empty line in
    >>the file, i.e.

    >
    >
    > No, it doesn't. Not if you've done something reasonable like
    > this:
    >
    > f = open('testdata','r')
    > while True:
    > s = f.readline()
    > if not s: break
    > print repr(s)
    >
    > or this:
    >
    > f = open('testdata','r')
    > s = f.readline()
    > while s:
    > print repr(s)
    > s = f.readline()
    >
    > Please post real, runnable code. You've done something wrong
    > and we've no way to guess what it was if you won't show us your
    > code.
    >


    I'm guessing it was runnable when he pasted it into google groups.

    James
     
    James Stroud, May 16, 2007
    #4
  5. Re: How do I tell the difference between the end of a text file,and an empty line in a text file?

    walterbyrd wrote:
    > Python's lack of an EOF character is giving me a hard time.
    >
    > I've tried:
    >
    > -----
    > s = f.readline()
    > while s:
    > .
    > .
    > s = f.readline()
    > --------
    >
    > and
    >
    > -------
    > s = f.readline()
    > while s != ''
    > .
    > .
    > s = f.readline()
    > -------
    >
    >
    > In both cases, the loop ends as soon it encounters an empty line in
    > the file, i.e.


    That's just not true. Did you try that code?

    >>> open('temp.txt', 'w').write('''\

    .... xxxxxxxxxx
    .... xxxxxxxxxxx
    .... xxxxxxx
    ....
    .... xxxxxxxxxxxxxx
    .... xxxxxxxxxx
    .... x
    .... ''')
    >>> while s:

    .... print s,
    .... s = f.readline()
    ....
    >>> f = open('temp.txt')
    >>> s = f.readline()
    >>> while s:

    .... print s,
    .... s = f.readline()
    ....
    xxxxxxxxxx
    xxxxxxxxxxx
    xxxxxxx

    xxxxxxxxxxxxxx
    xxxxxxxxxx
    x

    The file.readline() method returns '\n' for empty lines and '' for
    end-of-file.

    STeVe
     
    Steven Bethard, May 17, 2007
    #5
  6. walterbyrd

    Dan Bishop Guest

    On May 16, 4:47 pm, walterbyrd <> wrote:
    > Python's lack of an EOF character is giving me a hard time.
    >
    > I've tried:
    >
    > -----
    > s = f.readline()
    > while s:
    > .
    > .
    > s = f.readline()
    > --------
    >
    > and
    >
    > -------
    > s = f.readline()
    > while s != ''
    > .
    > .
    > s = f.readline()
    > -------
    >
    > In both cases, the loop ends as soon it encounters an empty line in
    > the file, i.e.
    >
    > xxxxxxxxxx
    > xxxxxxxxxxx
    > xxxxxxx
    > < - - - loop end here
    > xxxxxxxxxxxxxx
    > xxxxxxxxxx
    > x
    > < ---- loop should end here


    Use a "for s in f" loop instead.
     
    Dan Bishop, May 17, 2007
    #6
  7. walterbyrd

    Guest

    On May 16, 2:47 pm, walterbyrd <> wrote:
    > Python's lack of an EOF character is giving me a hard time.
    >
    > I've tried:
    >
    > -----
    > s = f.readline()
    > while s:
    > .
    > .
    > s = f.readline()
    > --------
    >
    > and
    >
    > -------
    > s = f.readline()
    > while s != ''
    > .
    > .
    > s = f.readline()
    > -------
    >
    > In both cases, the loop ends as soon it encounters an empty line in
    > the file, i.e.
    >
    > xxxxxxxxxx
    > xxxxxxxxxxx
    > xxxxxxx
    > < - - - loop end here
    > xxxxxxxxxxxxxx
    > xxxxxxxxxx
    > x
    > < ---- loop should end here


    Assuming f is initialized as in your example, try

    ---------
    for s in f:
    print s
    ---------

    casevh
     
    , May 17, 2007
    #7
  8. walterbyrd

    Asun Friere Guest

    On May 17, 7:47 am, walterbyrd <> wrote:
    > Python's lack of an EOF character is giving me a hard time.


    The difference is simply that an empty line contains a '\n' while EOF
    does not. If you strip() your line before testing you will have
    trouble. But the minimal cases you post (properly indented and with
    the missing ':' in place), should work (they just won't produce any
    output). Repairing the first , I'm using dots (aka stops, periods) for
    spaces here to stop the code getting munged :

    line = fobj.readline()
    while line :
    .....print line.strip()
    .....line = fobj.realine()

    This does work look at this output (and note the empty lines):
    line with stuff
    line with more stuff

    line after the empty line and before another

    last line

    In python it is more ideomatic to write this general kind of loop with
    a break statement, thus:

    while True :
    .....line = fobj.readline()
    .....if not line : break
    .....print line.strip()

    However since file has for a long time been an iterable the easiest
    and most readible way to write it is this:

    for line in fobj :
    .....print line.strip()

    Asun
     
    Asun Friere, May 17, 2007
    #8
    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. jakk
    Replies:
    4
    Views:
    12,385
  2. mary
    Replies:
    6
    Views:
    505
    Andrew Koenig
    Jan 19, 2005
  3. Jan Kokoska
    Replies:
    7
    Views:
    1,274
    John J. Lee
    Dec 6, 2003
  4. Replies:
    3
    Views:
    124
  5. PerlFAQ Server
    Replies:
    0
    Views:
    133
    PerlFAQ Server
    Feb 20, 2011
Loading...

Share This Page