473,385 Members | 1,536 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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

Jul 18 '05 #1
6 2685
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: yaipa | last post by:
What would be the common sense way of finding a binary pattern in a ..bin file, say some 200 bytes, and replacing it with an updated pattern of the same length at the same offset? Also, the...
20
by: Christian Stigen Larsen | last post by:
A signed int reserves one bit to signify whether a number is positive or negative. In light of this, a colleague asked me whether there existed an int in C++ that was -0, a zero with the negative...
3
by: Tron Thomas | last post by:
What does binary mode for an ofstream object do anyway? Despite which mode the stream uses, operator << writes numeric value as their ASCII representation. I read on the Internet that it is...
103
by: Steven T. Hatton | last post by:
§27.4.2.1.4 Type ios_base::openmode Says this about the std::ios::binary openmode flag: *binary*: perform input and output in binary mode (as opposed to text mode) And that is basically _all_ it...
9
by: Ching-Lung | last post by:
Hi all, I try to create a tool to check the delta (diff) of 2 binaries and create the delta binary. I use binary formatter (serialization) to create the delta binary. It works fine but the...
7
by: John Dann | last post by:
I'm trying to read some binary data from a file created by another program. I know the binary file format but can't change or control the format. The binary data is organised such that it should...
68
by: vim | last post by:
hello everybody Plz tell the differance between binary file and ascii file............... Thanks in advance vim
5
by: bwv539 | last post by:
I have to output data into a binary file, that will contain data coming from a four channel measurement instrument. Since those data have to be read from another C program somewhere else, the...
10
by: rory | last post by:
I can't seem to append a string to the end of a binary file. I'm using the following code: fstream outFile("test.exe", ios::in | ios::out | ios::binary | ios::ate | ios::app)...
16
by: Erwin Moller | last post by:
Why is a binary file executable? Is any binary file executable? Is only binary file executable? Are all executable files binary? What is the connection between the attribute of binary and that of...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.