Help reading binary data from files

jeff jeff at kalikstein.com
Tue Feb 6 17:34:01 EST 2007


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.  If
I throw away the zeros, I see my real data....sorry for the time waste




More information about the Python-list mailing list