How to use zipfile

Anton Vredegoor anton at vredegoor.doge.nl
Wed Nov 6 05:29:49 EST 2002


On Wed, 6 Nov 2002 15:36:33 +0800, "Jack Lau" <jack98lau at hotmail.com>
wrote:

>I am a beginner to learn python. Could anyone tell me how to use zipfile. I
>want to zip a file like "a.txt" and zipped it into "abc.zip". Then how can I
>do ?

Yes, I know this problem! It's a bootstrapping problem, the docs give
all kinds of detailed information but since there's no example script
it's hard to start experimenting with the functionality. So you have
to start googling, grepping sourcecode etc. to find an example script
to start from.

But ...

Do you realize you provided this list with the same kind of problem?
It's always easier to answer if there's at least a few lines of code
however erroneously it may be, to start answering from.

Now, to give some code to start experimenting with I just grabbed some
code from one of my projects as an example. Please try to produce a
few lines of code after having read it before asking the next question
...

Regards,
		AV.

def makeziparchive():
    sgfdir = 'sgffiles/'
    releasedir = '../release/'
    filenames = [ 'test38x38.sgf', 'test9x9.sgf', 'test13x13.sgf',
                       'Hon-1941-1.sgf', 'pygobo.pyw', 'sgfparser.py',
                       'last.pyw', 'readme.html','changes.html']
    z = ZipFile(releasedir + 'pygobo011.zip', 'w')
    for filename in filenames:
        name, ext = splitext(filename)
        if ext == '.sgf':
            filename = sgfdir + filename
            z.write('../'+filename,'pygobo/'+filename, ZIP_DEFLATED)
        else:
            z.write(filename,'pygobo/'+filename, ZIP_DEFLATED)
    z.close()




More information about the Python-list mailing list