Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: kent nyberg Newsgroups: comp.lang.python Subject: Problems using struct pack/unpack in files, and reading them. Date: Fri, 13 Nov 2015 14:20:45 -0500 Lines: 76 Message-ID: Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: news.uni-berlin.de io9aA/mMiVZRrRrac+I2wwGN/cu/+1i+WSm7RmYdyM6A== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'error:': 0.05; 'great.': 0.07; 'tmp': 0.07; 'command.': 0.09; 'second.': 0.09; 'subject:files': 0.09; 'subject:using': 0.09; 'unpack': 0.09; 'read.': 0.13; 'def': 0.13; 'argument': 0.15; 'skip:p 40': 0.15; 'variables': 0.15; '#we': 0.16; 'doesnt': 0.16; 'evening.': 0.16; 'file.close()': 0.16; 'file?': 0.16; 'readable': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'subject:Problems': 0.16; 'string': 0.17; 'bytes': 0.18; 'duplicate': 0.18; 'skip:l 30': 0.18; 'first,': 0.20; 'file.': 0.22; 'pass': 0.22; 'bit': 0.23; 'seems': 0.23; 'eager': 0.23; 'this:': 0.23; 'second': 0.24; '(most': 0.24; 'written': 0.24; 'header:User-Agent:1': 0.26; 'command': 0.26; 'question': 0.27; 'correct': 0.28; 'function': 0.28; 'once.': 0.29; 'read,': 0.29; 'subject:/': 0.30; 'that.': 0.30; 'filled': 0.30; 'error.': 0.31; 'another': 0.32; 'run': 0.33; 'problem': 0.33; 'surely': 0.33; 'traceback': 0.33; 'file': 0.34; 'add': 0.34; 'skip:c 30': 0.35; 'trouble': 0.35; 'next': 0.35; 'set.': 0.35; 'something': 0.35; 'but': 0.36; 'should': 0.36; 'to:addr:python-list': 0.36; 'two': 0.37; 'there,': 0.37; 'charset:us-ascii': 0.37; 'list.': 0.37; 'skip:p 20': 0.38; 'google': 0.39; 'test': 0.39; 'data': 0.39; 'to:addr:python.org': 0.40; 'where': 0.40; 'some': 0.40; 'share': 0.61; 'skip:u 10': 0.61; 'per': 0.62; '30,': 0.63; 'to,': 0.63; 'deeply': 0.64; 'received:178': 0.72; 'dark': 0.84; 'pack,': 0.84; 'subject:pack': 0.84 Content-Disposition: inline User-Agent: Mutt/1.5.23 (2014-03-12) X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Xref: csiph.com comp.lang.python:98757 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 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.