Writing binary files
Alex Martelli
aleax at aleax.it
Fri Sep 14 05:39:16 EDT 2001
<leind at sofcom.com> wrote in message
news:20010914.045538.373028728.15936 at taurus.bull...
> I have a large amount of binary data in memory (am image)
> and I'm trying to write it to a file. But I can't find a simple
> example showing how this is done.
What Python data type do you have all of this data in?
Built-in file objects have a write method that accepts
a string (and a writelines method that accepts a sequence
of strings and writes them out one after another: don't
get misled by the method's name, it doesn't _have_ to
be lines). Actually any Python object meeting the
(read-only) "buffer" interface will do, but that is not
well documented nor widely supported -- in practice,
strings are what's easy to write to files.
The file needs to be opened as mode "wb" (or "ab"
for append, etc) to _ensure_ no translation is done
for you (not a problem on Unix-like machines, in fact,
but it's a good habit to use "wb" &c anyway).
> I just need to write the data "as is" to a file.
>
> I've looked at the 'struct' module but that doesn't
> look like what I need. Also the 'pickle' module
> appears to be used for formating binary data. Again,
> not what I need.
struct makes a string out of other kinds of data in
a structured way (and viceversa) according to a
specified format. The built-in array module can
also serve for similar needs, but for sequences of
homogeneous elementary data (bytes, halfwords, &c).
Pickle uses a Python-specific formatting (both a
binary one and a textual one are supported) and thus,
while very useful when the data only need to be read
back by Python afterwards, it's not what you need if
_other_ applications must also access the data.
> Does python have the ablitiy to open a file and write
> binary data to it?
Sure! Just put the data in the desired binary format
in a string or other buffer-supporting kind of object
(e.g., array.array) and ensure the target file has
been opened for "wb", then methods write and writelines
do just what you want.
> I'm running under Mandrake linux using python 2.0
Then strictly speaking you don't need the b in "wb",
but use it anyway, it doesn't hurt and makes your
code more portable!-)
Let's give an example. Say I have computed the data
I'm interested in and placed it in a Python list of
Python objects, e.g.:
>>> mydata=range(33,57)
>>> mydata
[33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45,
46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56]
Now, say I want 25 bytes with these numeric values to
become the contents of binary file binfil.dat. Doing
it via a string is probably simplest, though not
maximally-effective:
>>> mystr = ''.join(map(chr,mydata))
Let's check the string is indeed what we want:
>>> mystr
'!"#$%&\'()*+,-./012345678'
Yep. So, write it to the file:
>>> flob=open('binfil.dat','wb')
>>> flob.write(mystr)
>>> flob.close()
That's it! Of course, we have many alternatives --
and if our original data isn't in a Python list of
numerical byte values, we'll need a somewhat different
approach, but I hope the basic ideas are clear enough.
Alex
More information about the Python-list
mailing list