Help reading binary data from files

John Machin sjmachin at lexicon.net
Tue Feb 6 17:47:33 EST 2007


On Feb 7, 9:34 am, "jeff" <j... at kalikstein.com> wrote:
> On Feb 6, 4:01 pm, "jeff" <j... at kalikstein.com> wrote:
>
>
>
> > 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




More information about the Python-list mailing list