How to Store Binary data in a *.py module

Chris Barker Chris.Barker at
Wed Jun 19 21:52:09 CEST 2002

I'd like to be able to store binary data in a python module file.

The motivation behind this is that I'd like to be able to distribute an
app with some of it's support files included in python modules, rather
than as separate files. This makes it easier to tao pas around, saves
some path issues, etc. It would also allow Py2exe to put it all in one
package. At the moment, if you have some data files to be read, you have
to distribute them separately.

I know a Python string can contain an arbitrary set of bytes, but I
don't know how to express binary data as a literal. If I were to try to
use r" ..." and I happened to have a byte that matched the ", it would
be mis-parsed. I'm imaging that UUencoding or something might solve my
problem, but I don't know anything about that.

Anyone have an idea??

BTW: this brings up the idea of having some kind of "resource file"
simulation in python. What I'm imagining is a way to build a module that
could contain a bunch of file-like objects that could contain any
arbitrary data, and be accessed like a file from Python. I suppose if I
figure out the above, the rest of it would be pretty easy...


Christopher Barker, Ph.D.
NOAA/OR&R/HAZMAT         (206) 526-6959   voice
7600 Sand Point Way NE   (206) 526-6329   fax
Seattle, WA  98115       (206) 526-6317   main reception

Chris.Barker at

More information about the Python-list mailing list