EOF for binary?

F

flamesrock

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
 
R

Roy Smith

"flamesrock said:
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.
 
F

flamesrock

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
 
M

Marc 'BlackJack' Rintsch

flamesrock said:
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.
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.
28190L

Ciao,
Marc 'BlackJack' Rintsch
 
D

Dennis Lee Bieber

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)

--
 
F

flamesrock

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

Dennis Lee Bieber

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>}

--
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top