470,826 Members | 1,809 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,826 developers. It's quick & easy.

Help reading binary data from files

I am stumped trying to read binary data from simple files. Here is a
code snippet, where I am trying to simply print little-endian encoded
data from files in a directory.

for name in os.listdir(DOWNLOAD_DIR):
filename = s.path.join(DOWNLOAD_DIR, name)
if os.path.isfile(filename):
f = open(filename, 'rb')
while True:
ele = unpack('<h', f.read(2))[0]
print ele
When the code runs, 0 is always the data printed, but the data files
are not all zero.

Any quick tips?

thanks

Feb 6 '07 #1
5 2030
On 2007-02-06, jeff <je**@kalikstein.comwrote:
I am stumped trying to read binary data from simple files. Here is a
code snippet, where I am trying to simply print little-endian encoded
data from files in a directory.

for name in os.listdir(DOWNLOAD_DIR):
filename = s.path.join(DOWNLOAD_DIR, name)
if os.path.isfile(filename):
f = open(filename, 'rb')
while True:
ele = unpack('<h', f.read(2))[0]
print ele
When the code runs, 0 is always the data printed, but the data files
are not all zero.

Any quick tips?
What are the f.read(2) calls returning?

--
Grant Edwards grante Yow! Hello, GORRY-O!! I'm
at a GENIUS from HARVARD!!
visi.com
Feb 6 '07 #2
En Tue, 06 Feb 2007 19:01:20 -0300, jeff <je**@kalikstein.comescribió:
I am stumped trying to read binary data from simple files. Here is a
code snippet, where I am trying to simply print little-endian encoded
data from files in a directory.

for name in os.listdir(DOWNLOAD_DIR):
filename = s.path.join(DOWNLOAD_DIR, name)
if os.path.isfile(filename):
f = open(filename, 'rb')
while True:
ele = unpack('<h', f.read(2))[0]
print ele
When the code runs, 0 is always the data printed, but the data files
are not all zero.
Looks fine to me...

--
Gabriel Genellina

Feb 6 '07 #3
On Feb 6, 4:01 pm, "jeff" <j...@kalikstein.comwrote:
I am stumped trying to read binary data from simple files. Here is a
code snippet, where I am trying to simply print little-endian encoded
data from files in a directory.

for name in os.listdir(DOWNLOAD_DIR):
filename = s.path.join(DOWNLOAD_DIR, name)
if os.path.isfile(filename):
f = open(filename, 'rb')
while True:
ele = unpack('<h', f.read(2))[0]
print ele

When the code runs, 0 is always the data printed, but the data files
are not all zero.

Any quick tips?

thanks
Wow, supreme stupidity on my part. It turns out that there were a lot
of zeros at the beginning of the file, and the slowness of the console
just showed me the zero data during the test time of ~ 10 seconds. If
I throw away the zeros, I see my real data....sorry for the time waste

Feb 6 '07 #4
On Feb 7, 9:01 am, "jeff" <j...@kalikstein.comwrote:
I am stumped trying to read binary data from simple files. Here is a
code snippet, where I am trying to simply print little-endian encoded
data from files in a directory.

for name in os.listdir(DOWNLOAD_DIR):
filename = s.path.join(DOWNLOAD_DIR, name)
if os.path.isfile(filename):
f = open(filename, 'rb')
while True:
ele = unpack('<h', f.read(2))[0]
print ele

When the code runs, 0 is always the data printed, but the data files
are not all zero.

Any quick tips?
Looks to me like it should work -- at least until it hits the end of
the first file. What do you expect to happen at the end of the first
file?? Or did you so snippetise the code so that even if the missing
import statements are added back, it still won't get into the 2nd
file?

I suggest a few more print statements, so that you can see what is
happening.
Try something like this (untested):

f = open(filename, 'rb')
print "Opened", filename
while True:
buff = f.read(2)
if not buff: break # EOF
if len(buff) == 1:
print repr(buff), ord(buff)
break
ele = unpack('<h', buff)[0]
print repr(buff), ele

Print this, cut it out, and paste it to the inside of your hat:

*repr() is your friend*

HTH,
John

Feb 6 '07 #5
On Feb 7, 9:34 am, "jeff" <j...@kalikstein.comwrote:
On Feb 6, 4:01 pm, "jeff" <j...@kalikstein.comwrote:
I am stumped trying to read binary data from simple files. Here is a
code snippet, where I am trying to simply print little-endian encoded
data from files in a directory.
for name in os.listdir(DOWNLOAD_DIR):
filename = s.path.join(DOWNLOAD_DIR, name)
if os.path.isfile(filename):
f = open(filename, 'rb')
while True:
ele = unpack('<h', f.read(2))[0]
print ele
When the code runs, 0 is always the data printed, but the data files
are not all zero.
Any quick tips?
thanks

Wow, supreme stupidity on my part. It turns out that there were a lot
of zeros at the beginning of the file, and the slowness of the console
just showed me the zero data during the test time of ~ 10 seconds.
That's a rather severe case of premature emailisation :-)
If
I throw away the zeros, I see my real data....sorry for the time waste
Some further suggestions (not mutually exclusive):
1. redirect the console to a file
2. write more per line
3. download a free or shareware gadget that will show you the contents
of a file in hex and char
4. You may want/need to write yourself a better dumper that's tailored
to the type of files that you are loooking at, e.g.

498: 0031 FONT len = 001e (30)
502: b4 00 00 00 08 00 90 01 00 00 00 00 00 a5 07 01 ?~~~?~??
~~~~~???
518: 56 00 65 00 72 00 64 00 61 00 6e 00 61 00
V~e~r~d~a~n~a~
532: 041e FORMAT len = 001e (30)
536: 05 00 19 00 00 23 2c 23 23 30 5c 20 22 44 4d 22 ?~?
~~#,##0\ "DM"
552: 3b 5c 2d 23 2c 23 23 30 5c 20 22 44 4d 22 ;\-#,##0\
"DM"
566: 041e FORMAT len = 0023 (35)
570: 06 00 1e 00 00 23 2c 23 23 30 5c 20 22 44 4d 22 ?~?
~~#,##0\ "DM"
586: 3b 5b 52 65 64 5d 5c 2d 23 2c 23 23 30 5c 20 22 ;[Red]\-
#,##0\ "
602: 44 4d 22 DM"

HTH,
John

Feb 6 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Olivier Maurice | last post: by
2 posts views Thread by Brian | last post: by
3 posts views Thread by abmd.tw | last post: by
50 posts views Thread by Michael Mair | last post: by
1 post views Thread by Jón Sveinsson | last post: by
6 posts views Thread by arne.muller | last post: by
5 posts views Thread by Sam | last post: by
reply views Thread by mihailmihai484 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.