file object and eof

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

    #1

    file object and eof

    I open a file in python by
    f = open('filename' , mode='rb')

    how can I tell if I am at the end of file?
    f.eof() isn't implmented.

    How can I implement its functionallity?

    Thanks.
    B.

  • Marc 'BlackJack' Rintsch

    #2
    Re: file object and eof

    In <1155660586.941 712.114410@b28g 2000cwb.googleg roups.com>, KraftDiner
    wrote:
    I open a file in python by
    f = open('filename' , mode='rb')
    >
    how can I tell if I am at the end of file?
    f.eof() isn't implmented.
    >
    How can I implement its functionallity?
    Try to read something. If the empty string is returned you are at the end
    of the file. Or write a function that uses `os.path.filesi ze()` and the
    `tell()` method of the file.

    Ciao,
    Marc 'BlackJack' Rintsch

    Comment

    • Grant Edwards

      #3
      Re: file object and eof

      On 2006-08-15, KraftDiner <bobrien18@yaho o.comwrote:
      I open a file in python by
      f = open('filename' , mode='rb')
      >
      how can I tell if I am at the end of file?
      I don't believe you can unless you try to read from the file.
      f.eof() isn't implmented.
      >
      How can I implement its functionallity?
      You don't, generally.

      There's probably a better, "more Pythonic" way to accomplish
      your goal, but you're going to have to tell us what it is.

      --
      Grant Edwards grante Yow! But was he mature
      at enough last night at the
      visi.com lesbian masquerade?

      Comment

      • KraftDiner

        #4
        Re: file object and eof


        Grant Edwards wrote:
        On 2006-08-15, KraftDiner <bobrien18@yaho o.comwrote:
        >
        I open a file in python by
        f = open('filename' , mode='rb')

        how can I tell if I am at the end of file?
        >
        I don't believe you can unless you try to read from the file.
        >
        f.eof() isn't implmented.

        How can I implement its functionallity?
        >
        You don't, generally.
        >
        There's probably a better, "more Pythonic" way to accomplish
        your goal, but you're going to have to tell us what it is.
        >
        how about?!:

        def eof(fileobj):
        curloc = fileobj.tell()
        ofloc = fileobj.seek(0, 2)
        fileobj.seek(cu rloc, 0)
        if ofloc >= curloc:
        return True
        return False

        Comment

        • Marc 'BlackJack' Rintsch

          #5
          Re: file object and eof

          In <1155668786.644 843.192830@m79g 2000cwm.googleg roups.com>, KraftDiner
          wrote:
          f.eof() isn't implmented.
          >
          How can I implement its functionallity?
          >>
          >You don't, generally.
          >>
          >There's probably a better, "more Pythonic" way to accomplish
          >your goal, but you're going to have to tell us what it is.
          >>
          >
          how about?!:
          >
          def eof(fileobj):
          curloc = fileobj.tell()
          ofloc = fileobj.seek(0, 2)
          fileobj.seek(cu rloc, 0)
          if ofloc >= curloc:
          return True
          return False
          Works only for files that support `tell()` and `seek()`. What's your use
          case for `eof()`?

          Ciao,
          Marc 'BlackJack' Rintsch

          Comment

          • sjdevnull@yahoo.com

            #6
            Re: file object and eof

            KraftDiner wrote:
            how about?!:
            >
            def eof(fileobj):
            curloc = fileobj.tell()
            ofloc = fileobj.seek(0, 2)
            fileobj.seek(cu rloc, 0)
            if ofloc >= curloc:
            return True
            return False
            1. At least on python 2.3, seek always returns None--did that change in
            2.4? I added a tell() in there to fix that.
            2. You have your test reversed. It'll always return True as is.
            3. Even if you fix that, it's limited utility depending on file mode
            and whether the file object is seekable.

            What's your actual problem? There's almost certainly a better way to
            do it.

            Comment

            • Steve Holden

              #7
              Re: file object and eof

              KraftDiner wrote:
              [...]
              how about?!:
              >
              def eof(fileobj):
              curloc = fileobj.tell()
              ofloc = fileobj.seek(0, 2)
              fileobj.seek(cu rloc, 0)
              if ofloc >= curloc:
              return True
              return False
              >
              Ignoring the errors in your file manipulation, think about replacing the
              last three lines with

              return ofloc >= curloc

              regards
              Steve
              --
              Steve Holden +44 150 684 7255 +1 800 494 3119
              Holden Web LLC/Ltd http://www.holdenweb.com
              Skype: holdenweb http://holdenweb.blogspot.com
              Recent Ramblings http://del.icio.us/steve.holden

              Comment

              • Fredrik Lundh

                #8
                Re: file object and eof

                KraftDiner wrote:
                how about?!:
                >
                def eof(fileobj):
                curloc = fileobj.tell()
                ofloc = fileobj.seek(0, 2)
                fileobj.seek(cu rloc, 0)
                if ofloc >= curloc:
                return True
                return False
                this doesn't work with text files on platforms that translates line
                endings, it doesn't work with streams, it's not very efficient, and as
                written, it doesn't work at all. and it's entirely pointless, since you
                need to check the return value from "read" anyway.

                </F>

                Comment

                Working...