EOF for binary?

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

    #1

    EOF for binary?

    Hi,

    So if I understand correctly, there are NO eof characters in any binary
    file. If so, is there an easier way to check for the end of the file
    than:

    os.path.getsize (infile) <= infile.tell()

    Because that returns the error:
    # File "/usr/lib/python2.3/posixpath.py", line 142, in getsize
    # return os.stat(filenam e).st_size
    #TypeError: coercing to Unicode: need string or buffer, file found
    for me.


    -thanks in advance
    flamesrock

  • Roy Smith

    #2
    Re: EOF for binary?

    In article <1105147072.909 665.242360@z14g 2000cwz.googleg roups.com>,
    "flamesrock " <flamesrock@gma il.com> wrote:
    [color=blue]
    > Hi,
    >
    > So if I understand correctly, there are NO eof characters in any binary
    > file. If so, is there an easier way to check for the end of the file
    > than:
    >
    > os.path.getsize (infile) <= infile.tell()[/color]

    How you detect EOF depends on how you're reading the file. For example,
    read() indicates EOF by returning fewer characters than you asked for
    (possibly zero).

    Checking the length of a file is perilous. Files change size. File
    sizes don't have much meaning on things like network sockets to begin
    with.

    Comment

    • flamesrock

      #3
      Re: EOF for binary?

      Thanks!

      I don't know why, but the most innefficient and unmaintanable means of
      doing something usually pops into my head before anything else. I
      solved this by going

      elif len(header) < 8:
      break

      Comment

      • Marc 'BlackJack' Rintsch

        #4
        Re: EOF for binary?

        In <1105147072.909 665.242360@z14g 2000cwz.googleg roups.com>, flamesrock
        wrote:
        [color=blue]
        > os.path.getsize (infile) <= infile.tell()
        >
        > Because that returns the error:
        > # File "/usr/lib/python2.3/posixpath.py", line 142, in getsize
        > # return os.stat(filenam e).st_size
        > #TypeError: coercing to Unicode: need string or buffer, file found
        > for me.[/color]

        This error message gives a good hint what's wrong with your code snippet.
        [color=blue][color=green][color=darkred]
        >>> f = open('tmp.txt')
        >>> os.path.getsize (f)[/color][/color][/color]
        Traceback (most recent call last):
        File "<stdin>", line 1, in ?
        File "/usr/lib/python2.3/posixpath.py", line 142, in getsize
        return os.stat(filenam e).st_size
        TypeError: coercing to Unicode: need string or buffer, file found

        Something expected a string instead of a file object.
        [color=blue][color=green][color=darkred]
        >>> os.path.getsize ("tmp.txt")[/color][/color][/color]
        28190L

        Ciao,
        Marc 'BlackJack' Rintsch

        Comment

        • Dennis Lee Bieber

          #5
          Re: EOF for binary?

          On 7 Jan 2005 17:17:52 -0800, "flamesrock " <flamesrock@gma il.com>
          declaimed the following in comp.lang.pytho n:[color=blue]
          >
          > os.path.getsize (infile) <= infile.tell()
          >
          > Because that returns the error:
          > # File "/usr/lib/python2.3/posixpath.py", line 142, in getsize
          > # return os.stat(filenam e).st_size
          > #TypeError: coercing to Unicode: need string or buffer, file found
          > for me.
          >[/color]
          Sounds reasonable...

          infile.tell() is a method on a /file type/

          os.path.getsize () wants a file-path/name. IE, it wants you to
          pass it whatever you fed (the xxx) to
          infile = open(xxx)

          --[color=blue]
          > =============== =============== =============== =============== == <
          > wlfraed@ix.netc om.com | Wulfraed Dennis Lee Bieber KD6MOG <
          > wulfraed@dm.net | Bestiaria Support Staff <
          > =============== =============== =============== =============== == <
          > Home Page: <http://www.dm.net/~wulfraed/> <
          > Overflow Page: <http://wlfraed.home.ne tcom.com/> <[/color]

          Comment

          • flamesrock

            #6
            Re: EOF for binary?

            ahh..that does make sense. But maybe getsize() should have a way of
            inferring what file is specified. I might actually submit a request..

            Comment

            • Dennis Lee Bieber

              #7
              Re: EOF for binary?

              On 8 Jan 2005 14:47:34 -0800, "flamesrock " <flamesrock@gma il.com>
              declaimed the following in comp.lang.pytho n:
              [color=blue]
              > ahh..that does make sense. But maybe getsize() should have a way of
              > inferring what file is specified. I might actually submit a request..[/color]

              But then it should be a method of file, not of os.path

              ie:
              infile = open(my_file_na me)
              sz = infile.getsize( )

              os.path is primarily concerned with directory
              information, not with the contents of an open file. {Though on LS-DOS,
              the C file pointer structure ended up in dual use... somehow, when the
              file is open, it held read/write pointers, etc. but when closed the
              filename was stuffed back into it <G>}

              --[color=blue]
              > =============== =============== =============== =============== == <
              > wlfraed@ix.netc om.com | Wulfraed Dennis Lee Bieber KD6MOG <
              > wulfraed@dm.net | Bestiaria Support Staff <
              > =============== =============== =============== =============== == <
              > Home Page: <http://www.dm.net/~wulfraed/> <
              > Overflow Page: <http://wlfraed.home.ne tcom.com/> <[/color]

              Comment

              Working...