By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,427 Members | 1,379 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,427 IT Pros & Developers. It's quick & easy.

EOF for binary?

P: n/a
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

Jul 18 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
In article <11**********************@z14g2000cwz.googlegroups .com>,
"flamesrock" <fl********@gmail.com> 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.
Jul 18 '05 #2

P: n/a
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

Jul 18 '05 #3

P: n/a
In <11**********************@z14g2000cwz.googlegroups .com>, 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
Jul 18 '05 #4

P: n/a
On 7 Jan 2005 17:17:52 -0800, "flamesrock" <fl********@gmail.com>
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)

-- ================================================== ============ <
wl*****@ix.netcom.com | Wulfraed Dennis Lee Bieber KD6MOG <
wu******@dm.net | Bestiaria Support Staff <
================================================== ============ <
Home Page: <http://www.dm.net/~wulfraed/> <
Overflow Page: <http://wlfraed.home.netcom.com/> <

Jul 18 '05 #5

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

Jul 18 '05 #6

P: n/a
On 8 Jan 2005 14:47:34 -0800, "flamesrock" <fl********@gmail.com>
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>}

-- ================================================== ============ <
wl*****@ix.netcom.com | Wulfraed Dennis Lee Bieber KD6MOG <
wu******@dm.net | Bestiaria Support Staff <
================================================== ============ <
Home Page: <http://www.dm.net/~wulfraed/> <
Overflow Page: <http://wlfraed.home.netcom.com/> <

Jul 18 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.