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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • walterbyrd

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

    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

  • James Stroud

    #2
    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_wit h_s(s)


    James

    Comment

    • Grant Edwards

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

      On 2007-05-16, walterbyrd <walterbyrd@ina me.comwrote:
      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?

      Comment

      • James Stroud

        #4
        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 <walterbyrd@ina me.comwrote:
        >
        >
        >>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

        Comment

        • Steven Bethard

          #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.tx t', '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

          Comment

          • Dan Bishop

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

            On May 16, 4:47 pm, walterbyrd <walterb...@ina me.comwrote:
            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.

            Comment

            • casevh@gmail.com

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

              On May 16, 2:47 pm, walterbyrd <walterb...@ina me.comwrote:
              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

              Comment

              • Asun Friere

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

                On May 17, 7:47 am, walterbyrd <walterb...@ina me.comwrote:
                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

                Comment

                Working...