Path: csiph.com!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail From: Laura Creighton Newsgroups: comp.lang.python Subject: Re: Problems using struct pack/unpack in files, and reading them. Date: Fri, 13 Nov 2015 21:15:23 +0100 Lines: 110 Message-ID: References: <20151113192045.GA9913@z-sverige.nu> Mime-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: quoted-printable X-Trace: news.uni-berlin.de f55Wu1n9YlUKx9NToK9+5Ay2P6toqlMlmanl0TRzdoXQ== 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; 'binary': 0.05; 'error:': 0.05; 'great.': 0.07; 'tmp': 0.07; 'cc:addr :python-list': 0.09; 'command.': 0.09; 'received:openend.se': 0.09; 'received:theraft.openend.se': 0.09; 'second.': 0.09; 'subject:files': 0.09; 'subject:using': 0.09; 'unpack': 0.09; 'read.': 0.13; 'argument': 0.15; 'skip:p 40': 0.15; 'variables': 0.15; '#we': 0.16; '999': 0.16; '>to': 0.16; 'cc:addr:lac': 0.16; 'cc:addr:openend.se': 0.16; 'doesnt': 0.16; 'evening.': 0.16; 'file.close()': 0.16; 'file?': 0.16; 'from:addr:lac': 0.16; 'from:addr:openend.se': 0.16; 'from:name:laura creighton': 0.16; 'message-id:@fido.openend.se': 0.16; 'readable': 0.16; 'received:fido': 0.16; 'received:fido.openend.se': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'subject:Problems': 0.16; 'string': 0.17; 'bytes': 0.18; 'laura': 0.18; 'skip:l 30': 0.18; '2015': 0.20; 'cc:addr:python.org': 0.20; 'first,': 0.20; 'cc:2**1': 0.22; 'struct': 0.22; 'file.': 0.22; 'pass': 0.22; 'bit': 0.23; 'seems': 0.23; 'eager': 0.23; 'elements': 0.23; 'this:': 0.23; 'second': 0.24; '(most': 0.24; 'written': 0.24; 'command': 0.26; 'fri,': 0.27; 'question': 0.27; 'correct': 0.28; 'function': 0.28; '-0500,': 0.29; 'once.': 0.29; 'received:se': 0.29; 'cc:no real name:2**1': 0.29; 'read,': 0.29; 'subject:/': 0.30; 'that.': 0.30; 'url:mailman': 0.30; 'filled': 0.30; 'error.': 0.31; 'another': 0.32; 'run': 0.33; 'problem': 0.33; 'url:python': 0.33; 'surely': 0.33; 'url:listinfo': 0.34; 'file': 0.34; 'add': 0.34; 'skip:c 30': 0.35; 'trouble': 0.35; 'next': 0.35; 'i.e.': 0.35; 'nov': 0.35; 'set.': 0.35; 'skip:> 10': 0.35; 'something': 0.35; 'but': 0.36; 'should': 0.36; 'url:org': 0.36; 'subject:: ': 0.37; 'two': 0.37; 'there,': 0.37; 'charset:us- ascii': 0.37; 'list.': 0.37; 'things': 0.38; 'skip:p 20': 0.38; 'google': 0.39; 'test': 0.39; 'data': 0.39; 'url:mail': 0.40; 'where': 0.40; 'some': 0.40; 'skip:u 10': 0.61; 'header:Message- Id:1': 0.61; 'per': 0.62; '30,': 0.63; 'to,': 0.63; 'deeply': 0.64; '>def': 0.84; 'dark': 0.84; 'header:In-reply-to:1': 0.84; 'kent': 0.84; 'pack,': 0.84; 'subject:pack': 0.84; 'have.': 0.93 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=openend.se; s=default; t=1447445729; bh=d11Hc4xMOgHKPfodlfKALvNjq2eyOKX/q/F/N/JDPKo=; h=To:cc:From:Subject:In-reply-to:References:Date:From; b=hotIk0RZlhcaRuIyvWfp3YKfCkRQiGdjXUqqwUwEVecYMI6VkBHnYQJ/YFJ15Irf2 xl2QFa3Ei1F8y16GDU1EQu4tsRDyubr+XKur/j1ksAxsBFm5P6qIq0CtkRcOimmGaJ +YhlhvREnQHL2GWb5a8Uxon2b//jw6SWI5xWm9jQ= In-reply-to: <20151113192045.GA9913@z-sverige.nu> Comments: In-reply-to kent nyberg message dated "Fri, 13 Nov 2015 14:20:45 -0500." Content-ID: <28146.1447445723.1@fido> X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.3.9 (theraft.openend.se [82.96.5.2]); Fri, 13 Nov 2015 21:15:29 +0100 (CET) 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:98765 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 acros= s 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 =3D pack('>HH', 0b1000000000000000, 0b1000000100000001) > file =3D 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 r= ight now. > >This all works great. >Then I do this: > > >def LoadCommandAndReact(place_to_read): > global RegisterAX > = > tmp =3D place_to_read.read()[RegisterAX:calcsize('HH')] > klar =3D 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=3D+2 #We read two bytes per cycle. Becaus command is fir= st, 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 =3D unpack('>HH', tmp) >struct.error: unpack requires a string argument of length 4 > > > >Im having trouble understanding the error. unpack requires a string arg= ument 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 dupli= cates; should it not be readable the second >time aswell? I change the RegisterAX =3D+2. The amount I add should not m= atter, Its just my "offset" of where to read in the file. >The problem seems that the second time, unpack doesnt get the size of '>H= H' to read. But surely = >.read()[RegisterAX:calcsize('HH')] should assure that its fed with corre= ct data? > >I have filled the file with data of size '>HH' and first command reads th= at. 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 =3D struct.unpack("999H", B) Laura