Problems using struct pack/unpack in files, and reading them.

kent nyberg kent at z-sverige.nu
Fri Nov 13 15:15:10 EST 2015


On Fri, Nov 13, 2015 at 12:36:22PM -0700, Ian Kelly wrote:
> On Fri, Nov 13, 2015 at 12:20 PM, kent nyberg <kent at z-sverige.nu> wrote:
> > def LoadCommandAndReact(place_to_read):
> >     global RegisterAX
> >
> >     tmp = place_to_read.read()[RegisterAX:calcsize('HH')]
> 
> It looks like you're trying to get a slice of length 4 here, starting
> at the value of RegisterAX. What you're actually getting is a slice
> starting at the value of RegisterAX and ending at 4. You probably want
> this instead:
> 
>     tmp = place_to_read.read()[RegisterAX:RegisterAX + calcsize('HH')]
> -- 
> https://mail.python.org/mailman/listinfo/python-list


Even with that, it still gets wrong.
I also tried .read()[RegisterAX:RegisterAX+4]

As for the other suggestions you had in the second reply, I can comment on them later.
 For now though, I just want to be able to loop this specific function.

I do appriciate that you took the time. :)

What bothers me, is the error that says 
unpack requires a string argument of 4 bytes.
Im thinking in the line of arguments? Does unpack look at the 4 bytes it has read, and tell for some 
reason say that unpacking needs an argument of 4 bytes?  I know that I can set the arguments 
for unpack. I have done that.  It is: unpack('>HH', tmp).
So either '>HH' or tmp is wrong. Since '>HH' worked for the first run of the function, I assume its correct.
And as far as I know,   doing .read()[RegisterAX:RegisterAX:+4]  should read the following
4 bytes.

I have even tried doing .read(4),  since then python should manage where to start and  read 4?

Still it complains?



More information about the Python-list mailing list