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

Laura Creighton lac at openend.se
Fri Nov 13 15:15:23 EST 2015


In a message of Fri, 13 Nov 2015 14:20:45 -0500, kent nyberg writes:
>Hi there,
>Im deeply sorry for yet another question to this list.  I have come across a problem to which google seems not 
>to eager to supply the anwser.
>
>The problem is the following.
>First I do this:
>
>def setup_drive():
>    test = pack('>HH',  0b1000000000000000, 0b1000000100000001)
>    file = open('drive.bin', 'wb')
>    for x in range(640000):
>        file.write(test)
>    file.close()
>
>
>I have a pack, which I fill the file with. The data is not of interrest right now.
>
>This all works great.
>Then I do this:
>
>
>def LoadCommandAndReact(place_to_read):
>    global RegisterAX
>    
>    tmp = place_to_read.read()[RegisterAX:calcsize('HH')]
>    klar = unpack('>HH', tmp)
>
>    if place_to_read.closed:
>       print("Drive error. Drive closed.")
>    else:
>       pass
>
>    if checkfirstbit(klar[RegisterAX]):
>       print("First bit is set. Move Cmd.")
>       if (klar[0] & 0b0111111111111111):
>          print("Cmd ERROR: Multiple commands is set.")
>          pass
>       else:
>          #Here something to do with mv command.
>          print("Adress to read to, from the offset of the command")
>          print(klar[RegisterAX+1])  #Variable is always Offset+1          
>    else:
>       print("First bit is not set.")
>    #Change RegisterAX offset+2bytes, 
>    RegisterAX=+2   #We read two bytes per cycle. Becaus command is first, and variables are second.    
>
>
>
>
>This all works If I run the LoadCommand..  only once.
>But when I  run it for the second time,  it complains:
>
>Traceback (most recent call last):
>  File "core.py", line 98, in <module>
>    LoadCommandAndReact(readfile)
>  File "core.py", line 30, in LoadCommandAndReact
>    klar = unpack('>HH', tmp)
>struct.error: unpack requires a string argument of length 4
>
>
>
>Im having trouble understanding the error.   unpack requires a string argument of length 4.
>I have filled the file with data that should be readable, and is readable by the function 
>when I run it for the first time. And since the file is filled with duplicates; should it not be readable the second
>time aswell? I change the RegisterAX =+2. The amount I add should not matter, Its just my "offset" of where to read in the file.
>The problem seems that the second time, unpack doesnt get the size of '>HH' to read. But surely 
>.read()[RegisterAX:calcsize('HH')]  should assure that its fed with correct data?
>
>I have filled the file with data of size '>HH' and first command reads that.  So next read should read the next
>duplicate of that data that has been written to the file?
>
>
>I understand its hard to read, and a long email. So well, its just a cry out in the dark evening.  If some one can
>share some light.. I would be much happy about it.
>
>/Kent.
>-- 
>https://mail.python.org/mailman/listinfo/python-list

struct wants to know exactly how many of those things you have.
You only told it about one of them .... i.e.  If you want to unpack 999 
elements from binary data

data = struct.unpack("999H", B)

Laura





More information about the Python-list mailing list