Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: kent nyberg Newsgroups: comp.lang.python Subject: Re: Problems using struct pack/unpack in files, and reading them. Date: Fri, 13 Nov 2015 15:15:10 -0500 Lines: 37 Message-ID: References: <20151113192045.GA9913@z-sverige.nu> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: news.uni-berlin.de UbpwQ/J1KUpUwbZ38oWpRQVcNISOeOA/QaQI3JDdHikg== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.002 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'bytes.': 0.07; 'correct.': 0.07; 'tmp': 0.07; 'subject:files': 0.09; 'subject:using': 0.09; 'unpack': 0.09; 'unpacking': 0.09; 'python': 0.10; 'assume': 0.11; 'def': 0.13; 'argument': 0.15; 'skip:p 40': 0.15; 'instead:': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'subject:Problems': 0.16; 'unpack.': 0.16; 'wrote:': 0.16; 'string': 0.17; 'bytes': 0.18; 'skip:l 30': 0.18; '2015': 0.20; 'arguments': 0.22; 'function,': 0.22; 'trying': 0.22; 'second': 0.24; 'tried': 0.24; 'header:In-Reply-To:1': 0.24; 'header:User- Agent:1': 0.26; 'error': 0.27; 'fri,': 0.27; 'looks': 0.29; '-0700,': 0.29; '13,': 0.29; 'read,': 0.29; 'subject:/': 0.30; 'that.': 0.30; 'url:mailman': 0.30; 'probably': 0.31; 'says': 0.32; 'though,': 0.32; 'getting': 0.33; 'run': 0.33; 'url:python': 0.33; 'url:listinfo': 0.34; 'worked': 0.34; 'that,': 0.34; 'gets': 0.35; 'done': 0.35; 'nov': 0.35; 'comment': 0.35; 'should': 0.36; 'url:org': 0.36; 'to:addr:python-list': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'say': 0.37; 'charset:us-ascii': 0.37; 'starting': 0.37; 'doing': 0.38; 'does': 0.39; 'url:mail': 0.40; 'to:addr:python.org': 0.40; 'where': 0.40; 'still': 0.40; 'some': 0.40; 'skip:u 10': 0.61; 'ending': 0.63; 'received:178': 0.72; 'reply,': 0.72; '12:20': 0.84; 'kent': 0.84; 'subject:pack': 0.84 Content-Disposition: inline In-Reply-To: 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:98764 On Fri, Nov 13, 2015 at 12:36:22PM -0700, Ian Kelly wrote: > On Fri, Nov 13, 2015 at 12:20 PM, kent nyberg 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?