zipping a directory

Callie Bertsche c.bertsche at tecplot.com
Tue Oct 28 14:06:31 EDT 2008


Hey Python-ers,
I apologize if this is covered in the archives; I think I saw one
instance of it but I couldn't get the solution to work out.
I'm working on zipping an entire directory to one zip file. I can zip a
flat group of files, but when my code encounters a hierarchy of folders,
for some reason it starts to zip everything on my desktop (instead of
everything inside one folder directory on desktop).  Then it goes into
an infinite loop and barfs.
 
Here's what I have so far:
 
import zipfile
import glob, os
 
def main():
 
    directory = "test\*"
    (success,filename)=createZipFile(directory);
    if success == 1:
        print "Operation completed. All files written to zip."
    else:
        print "Operation failed."
 
def createZipFile(directory):
    zippedHelp = zipfile.ZipFile("help.zip", "w" ) 

    for entity in directory:
        if os.path.isfile(entity):
 
zippedHelp.write(entity,os.path.basename(entity),zipfile.ZIP_DEFLATED)
        else:
            addFolderToZip(zippedHelp,entity)
 
    zippedHelp.close()
    return (1,zippedHelp)
 

def addFolderToZip(zippedHelp,folder):
 
    for file in folder:
            if os.path.isfile(file):
                zippedHelp.write(file, os.path.basename(file),
zipfile.ZIP_DEFLATED)
            elif os.path.isdir(file):
                addFolderToZip(zippedHelp,file)
 

main()
 
Thanks for any help!! :)
Callie
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20081028/96becbbe/attachment.html>


More information about the Python-list mailing list