Python Forum

Full Version: including big file contents in python source
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
i have a couple use cases where i need to include some big file contents. because these use cases are very complex and have too many things going on to sanely discuss them here, i will, instead, present a simplistic useless case; the kind of thing you might see in a classroom environment.
file (
here is that overly simplistic purpose. i need to create a python source file the is a means to transport a big file contents so that running the script "unpacks" the file to where the program is run.

the question i need to ask points to this simplistic purpose. what is the the best way to efficiently put a big (a few kilobytes to a few megabytes) file contents into python source code, so that, in the simplistic case, with included logic code, invoking the script can write that contents to a file.

my first thought is to have many lines of continued concatenated string with data in base 16, base 64, or base 85. any other ideas?
You can also use uuencode
import urllib.request, codecs
table = bytes.maketrans(b'\\"', b'xy')
invtable = bytes.maketrans(b'xy', b'\\"')

def to_literal(bdata):
    s = codecs.encode(bdata, 'uu').translate(table).decode()
    return 'b"""' + s + '"""'

def from_literal(data):
    s = eval(data).translate(invtable)
    return codecs.decode(s, 'uu')

s = urllib.request.urlopen('https://www.w3.org').read()
print(to_literal(s))
assert s == from_literal(to_literal(s))
uuencode has single and double quotes in it. that can make it hard to use in source literals. but base85 doesn't have either.