else:
name = prefix + line[mlen:-1] # or make new output
print('creating:', name)
output = open(name, 'w')
if name == 'main': unpack(sys.argv[1])
These scripts are fairly basic, and this GUI part of the book assumes you’ve already
scanned the system tools chapters, so we won’t go into their code in depth. Variants
of these scripts appeared in the first edition of this book in 1996; I actually used them
early on in my Python career to bundle files before I could rely on tools like tar and zip
to be present on all the machines I used (and before Python grew tar and zip support
modules in its standard library). Their operation is straightforward—consider these
three text files:
C:\...\PP4E\Gui\ShellGui> type spam.txt
spam
Spam
SPAM
C:\...\PP4E\Gui\ShellGui> type eggs.txt
eggs
C:\...\PP4E\Gui\ShellGui> type ham.txt
h
a
m
When run from the command line, the packer script combines them into a single text
file, and the unpacker extracts them from there; the packer must take care to glob
(expand) filename patterns, because this isn’t done by default in Windows:
C:\...\PP4E\Gui\ShellGui> packer.py packed.txt *.txt
packing: eggs.txt
packing: ham.txt
packing: spam.txt
C:\...\PP4E\Gui\ShellGui> unpacker.py packed.txt
creating: new-eggs.txt
creating: new-ham.txt
creating: new-spam.txt
The result files have a unique name by default (with an added prefix to avoid accidental
overwrites, especially during testing), but you otherwise get back what you packed:
C:\...\PP4E\Gui\ShellGui> type new-spam.txt
spam
Spam
SPAM
C:\...\PP4E\Gui\ShellGui> type packed.txt
::::::::::::::::::::textpak=>eggs.txt
eggs
::::::::::::::::::::textpak=>ham.txt
618 | Chapter 10: GUI Coding Techniques