Including binary files in a .py file; advice needed

Joe Francia usenet at soraia.com
Wed Jun 4 23:53:05 EDT 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.
> 

You can use the uu module, though I'd suggest the base64 instead, as it 
can decode a string directly (uu requires a file-like object), and tends 
to produce somewhat smaller strings.

############

#careful not to include a leading newline in b64 string
ENCODED_GIF = \
'''R0lGODlhcwCKAPf/AJNtWolhToNdS7OEbGxLPbOMcraimHRTQ6iclkc4NntTQ725tnFiXFlJRb2o
m5x5ZUEuKod5dpt1YotkUqZzXVtCN6t9Zb2Se5ptWpiJiKyKdJOIer6bhMbGxlM2LoV0bToqJ3NL
blah blah etc...
'''

import base64
out_gif = file('/home/scaba/littleman.gif', 'wb')
out_gif.write(base64.decodestring(ENCODED_GIF))
out_gif.close()

#############

jf





More information about the Python-list mailing list