EOF for binary?

Discussion in 'Python' started by flamesrock, Jan 8, 2005.

  1. flamesrock

    flamesrock Guest

    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(filename).st_size
    #TypeError: coercing to Unicode: need string or buffer, file found
    for me.


    -thanks in advance
    flamesrock
     
    flamesrock, Jan 8, 2005
    #1
    1. Advertising

  2. flamesrock

    Roy Smith Guest

    In article <>,
    "flamesrock" <> wrote:

    > 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()


    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.
     
    Roy Smith, Jan 8, 2005
    #2
    1. Advertising

  3. flamesrock

    flamesrock Guest

    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
     
    flamesrock, Jan 8, 2005
    #3
  4. In <>, flamesrock
    wrote:

    > 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(filename).st_size
    > #TypeError: coercing to Unicode: need string or buffer, file found
    > for me.


    This error message gives a good hint what's wrong with your code snippet.

    >>> f = open('tmp.txt')
    >>> os.path.getsize(f)

    Traceback (most recent call last):
    File "<stdin>", line 1, in ?
    File "/usr/lib/python2.3/posixpath.py", line 142, in getsize
    return os.stat(filename).st_size
    TypeError: coercing to Unicode: need string or buffer, file found

    Something expected a string instead of a file object.

    >>> os.path.getsize("tmp.txt")

    28190L

    Ciao,
    Marc 'BlackJack' Rintsch
     
    Marc 'BlackJack' Rintsch, Jan 8, 2005
    #4
  5. On 7 Jan 2005 17:17:52 -0800, "flamesrock" <>
    declaimed the following in comp.lang.python:
    >
    > 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(filename).st_size
    > #TypeError: coercing to Unicode: need string or buffer, file found
    > for me.
    >

    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)

    --
    > ============================================================== <
    > | Wulfraed Dennis Lee Bieber KD6MOG <
    > | Bestiaria Support Staff <
    > ============================================================== <
    > Home Page: <http://www.dm.net/~wulfraed/> <
    > Overflow Page: <http://wlfraed.home.netcom.com/> <
     
    Dennis Lee Bieber, Jan 8, 2005
    #5
  6. flamesrock

    flamesrock Guest

    ahh..that does make sense. But maybe getsize() should have a way of
    inferring what file is specified. I might actually submit a request..
     
    flamesrock, Jan 8, 2005
    #6
  7. On 8 Jan 2005 14:47:34 -0800, "flamesrock" <>
    declaimed the following in comp.lang.python:

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


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

    ie:
    infile = open(my_file_name)
    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>}

    --
    > ============================================================== <
    > | Wulfraed Dennis Lee Bieber KD6MOG <
    > | Bestiaria Support Staff <
    > ============================================================== <
    > Home Page: <http://www.dm.net/~wulfraed/> <
    > Overflow Page: <http://wlfraed.home.netcom.com/> <
     
    Dennis Lee Bieber, Jan 8, 2005
    #7
    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. Reading binary file finding EOF

    , Dec 13, 2004, in forum: C Programming
    Replies:
    11
    Views:
    652
    Lawrence Kirby
    Dec 14, 2004
  2. Kobu
    Replies:
    10
    Views:
    627
    Keith Thompson
    Mar 4, 2005
  3. David Warner

    Testing for EOF in file with binary data

    David Warner, Apr 28, 2006, in forum: C Programming
    Replies:
    3
    Views:
    491
    Richard Tobin
    Apr 28, 2006
  4. SpreadTooThin

    ifstream eof not reporting eof?

    SpreadTooThin, Jun 13, 2007, in forum: C++
    Replies:
    10
    Views:
    697
    James Kanze
    Jun 15, 2007
  5. Jan Burse
    Replies:
    67
    Views:
    1,066
    Jan Burse
    Mar 14, 2012
Loading...

Share This Page