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

Ian Kelly ian.g.kelly at gmail.com
Fri Nov 13 14:45:42 EST 2015


As long as I'm replying to this, I see a few more issues to comment on:

On Fri, Nov 13, 2015 at 12:20 PM, kent nyberg <kent at z-sverige.nu> wrote:
>     if place_to_read.closed:
>        print("Drive error. Drive closed.")

You probably also want to break or return here. Even better: raise an
exception instead of printing.

That said, why would this ever be closed here, since you just read
from it two lines prior (which would have raised an exception if the
file were closed), and you haven't closed it in the meantime?

>     if checkfirstbit(klar[RegisterAX]):
>        print("First bit is set. Move Cmd.")
>        if (klar[0] & 0b0111111111111111):
>           print("Cmd ERROR: Multiple commands is set.")

Why do you pass the commands in a bit field if you're not going to
allow multiple commands to be set? Also, see above about
break/return/exception.

>           pass

Unnecessary.

>     #Change RegisterAX offset+2bytes,
>     RegisterAX=+2   #We read two bytes per cycle. Becaus command is first, and variables are second.

This sets RegisterAX to 2. Specifically, positive 2. If you want to
*add* 2, you probably meant to write:

    RegisterAX += 2

This also points out a good reason for using spaces around operators,
as "RegisterAX =+ 2" would have caused a SyntaxError and been more
easily detectable.



More information about the Python-list mailing list