Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #98773

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

Path csiph.com!xmission!news.snarked.org!news.linkpendium.com!news.linkpendium.com!panix!not-for-mail
From Grant Edwards <invalid@invalid.invalid>
Newsgroups comp.lang.python
Subject Re: Problems using struct pack/unpack in files, and reading them.
Date Fri, 13 Nov 2015 21:44:49 +0000 (UTC)
Organization PANIX Public Access Internet and UNIX, NYC
Lines 33
Message-ID <n25lkh$egl$1@reader1.panix.com> (permalink)
References <20151113192045.GA9913@z-sverige.nu> <CALwzidnL1N8efgJfM-EKTgqzmDjg2QwcZVVmDXJB2_QGyFnExA@mail.gmail.com> <20151113201510.GA10107@z-sverige.nu> <mailman.306.1447448484.16136.python-list@python.org> <n25k27$m37$1@reader1.panix.com> <mailman.309.1447450493.16136.python-list@python.org>
NNTP-Posting-Host 67-130-15-94.dia.static.qwest.net
X-Trace reader1.panix.com 1447451089 14869 67.130.15.94 (13 Nov 2015 21:44:49 GMT)
X-Complaints-To abuse@panix.com
NNTP-Posting-Date Fri, 13 Nov 2015 21:44:49 +0000 (UTC)
User-Agent slrn/1.0.2 (Linux)
Xref csiph.com comp.lang.python:98773

Show key headers only | View raw


On 2015-11-13, kent nyberg <kent@z-sverige.nu> wrote:

> Though, as many times before, the problem was due to misunderstanding
> of how python works. I assumed file.read()[xx:yy] was to be
> understood as, in the file, read from index xx to place yy.

Nope.

   First, the 'file.read()' part is evaluated.  That returns the
   entire contents of the file.

   Next, the '[xx:yy]' slice operation is done on the entire contents
   returned in the first step.  The slice operation retuns bytes xx
   through yy-1 (inclusive), and discards the rest of the data.

> Is it true then to say that every .read of a file, places an
> index-offset (?) from where the next read starts?

Yes.  a file object has a current index.  A read() operation always
starts at that index.  When you open the file, that index is 0.  Each
time you read from the file, the index is advanced past the data that
was read.  The seek() method sets that index to whatever you want.

> That is, I need to rewind the file, or else the read will start from
> where the last read stopped?

Exactly.

-- 
Grant Edwards               grant.b.edwards        Yow! Mr and Mrs PED, can I
                                  at               borrow 26.7% of the RAYON
                              gmail.com            TEXTILE production of the
                                                   INDONESIAN archipelago?

Back to comp.lang.python | Previous | NextPrevious in thread | Find similar | Unroll thread


Thread

Re: Problems using struct pack/unpack in files, and reading them. Ian Kelly <ian.g.kelly@gmail.com> - 2015-11-13 14:00 -0700
  Re: Problems using struct pack/unpack in files, and reading them. Grant Edwards <invalid@invalid.invalid> - 2015-11-13 21:17 +0000
    Re: Problems using struct pack/unpack in files, and reading them. kent nyberg <kent@z-sverige.nu> - 2015-11-13 16:34 -0500
      Re: Problems using struct pack/unpack in files, and reading them. Grant Edwards <invalid@invalid.invalid> - 2015-11-13 21:44 +0000

csiph-web