[Python编程(第4版)].(Programming.Python.4th.Edition).Mark.Lutz.文字版

(yzsuai) #1
>>> number = bytes[8:10]
>>> number
b'\x00\x03'
>>> struct.unpack('>h', number)
(3,)

Packed binary data crops up in many contexts, including some networking tasks, and
in data produced by other programming languages. Because it’s not part of every pro-
gramming job’s description, though, we’ll defer to the struct module’s entry in the
Python library manual for more details.


Random access files


Binary files also typically see action in random access processing. Earlier, we mentioned
that adding a + to the open mode string allows a file to be both read and written. This
mode is typically used in conjunction with the file object’s seek method to support
random read/write access. Such flexible file processing modes allow us to read bytes
from one location, write to another, and so on. When scripts combine this with binary
file modes, they may fetch and update arbitrary bytes within a file.


We used seek earlier to rewind files instead of closing and reopening. As mentioned,
read and write operations always take place at the current position in the file; files
normally start at offset 0 when opened and advance as data is transferred. The seek call
lets us move to a new position for the next transfer operation by passing in a byte offset.


Python’s seek method also accepts an optional second argument that has one of three
values—0 for absolute file positioning (the default); 1 to seek relative to the current
position; and 2 to seek relative to the file’s end. That’s why passing just an offset of 0
to seek is roughly a file rewind operation: it repositions the file to its absolute start. In
general, seek supports random access on a byte-offset basis. Seeking to a multiple of a
record’s size in a binary file, for instance, allows us to fetch a record by its relative
position.


Although you can use seek without + modes in open (e.g., to just read from random
locations), it’s most flexible when combined with input/output files. And while you
can perform random access in text mode, too, the fact that text modes perform Unicode
encodings and line-end translations make them difficult to use when absolute byte
offsets and lengths are required for seeks and reads—your data may look very different
when stored in files. Text mode may also make your data nonportable to platforms
with different default encodings, unless you’re willing to always specify an explicit
encoding for opens. Except for simple unencoded ASCII text without line-ends, seek
tends to works best with binary mode files.


To demonstrate, let’s create a file in w+b mode (equivalent to wb+) and write some data
to it; this mode allows us to both read and write, but initializes the file to be empty if
it’s already present (all w modes do). After writing some data, we seek back to file start
to read its content (some integer return values are omitted in this example again for
brevity):


File Tools | 153
Free download pdf