Including binary files in a .py file; advice needed

Geoffrey Talvola gtalvola at nameconnector.com
Fri Jun 6 15:37:57 CEST 2003


Gary Duncan wrote:
> Here's what I want to do; distribute a .py file (a GUI app) with
> an embedded (binary) .GIF file (a photo of me at 1 yo ;) rather than
> distribute both separately. For convenience, mainly.
> 
> If in a Unix shell script, I'd uuencode the .GIF and include the
> printable-ASCII file in it as a 'here' file. When executed the script
> would 'cat' it out to e.g. a /tmp file from the main body of the
> script, then uudecode the /tmp file to revert to the original .GIF
> file. 
> 
> I imagine in Python, one could place the uuencoded file (lines)
> as a triple-quoted string, then uudecode it the .py program.
> 
> Anything better ?

Use repr().  For example:

>>> image = open('foo.jpeg', 'rb').read()
>>> image_repr = repr(image)
>>> out = open('foo.py', 'w')
>>> print >>out, 'foo =', image_repr
>>> out.close()
>>>
>>> import foo
>>> foo.foo == image
1
>>>

- Geoff





More information about the Python-list mailing list