Help reading binary data from files

J

jeff

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
 
G

Grant Edwards

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?
 
G

Gabriel Genellina

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...
 
J

jeff

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
 
J

John Machin

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
 
J

John Machin

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?

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
 

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,995
Messages
2,570,228
Members
46,818
Latest member
SapanaCarpetStudio

Latest Threads

Top