struct problem
Gordon McMillan
gmcm at hypernet.com
Thu May 27 23:00:51 EDT 1999
Paul Stillwell writes:
> I'm not sure why the following code doesn't work. I am reading a
> file and want to skip over the first 2 bytes of the file and read
> the next 2 bytes. So, I am doing this:
>
> from struct import *
>
> HeaderFormat = '2x2B'
> HeaderLength = calcsize(HeaderFormat)
> File = open('d:\starview', 'rb')
> Header = File.read(HeaderLength)
> (NumDomains, Version) = unpack(HeaderFormat, Header)
>
> but when I run this code I get:
>
> (NumDomains, Version) = unpack(HeaderFormat, Header)
> error: unpack str size does not match format
>
> So, what am I doing wrong? I can read the first 2 bytes into don't
> care variables, but why should I have to? I would think the pad
> byte format would skip the first 2 bytes and just return the 2 bytes
> I am interested in.
There's nothing wrong with your struct code. That leaves
File = open('d:\starview', 'rb')
or
Header = File.read(HeaderLength)
as the problem. Symptom indicates you didn't read 4 bytes. Perusal
reveals a single backslash in the filename, (though you didn't get an
IOError).
One or the other.
- Gordon
More information about the Python-list
mailing list