Vetrivelkumar Pandi
Vetrivelkumar Pandi

Reputation: 249

zip creation issue with Python 2.6.6

I have code to create a test.zip using python 2.6.6 as below, and creating a zip too.

But inside test.zip I could see one more test.zip, with 0/1 MB size. I am not sure how to fix or what I am missing.

def createZip(path, ziph):
    # ziph is zipfile handle
    for folder, subfolders, files in os.walk(path):
        for file in files:
            ziph.write(os.path.join(folder, file), os.path.relpath(os.path.join(folder,file),path), compress_type = zipfile.ZIP_DEFLATED)


zipf = zipfile.ZipFile('/home/stbUpgrade/tmp/downLoad/test.zip', 'w')
createZip('/home/stbUpgrade/tmp/downLoad/', zipf)
zipf.close()

Upvotes: 2

Views: 469

Answers (1)

clemens
clemens

Reputation: 17722

You zip the folder into a file, which is part of that folder. zipfile.ZipFile() will create an empty zip file, and thus you get an empty zip file in your test.zip.

You should place your test.zip into a folder outside of your zipped folder (e.g. your tmp folder).

BTW: You should use with for the case of errors:

with zipfile.ZipFile('/home/stbUpgrade/tmp/test.zip', 'w') as zipf:
    createZip('/home/stbUpgrade/tmp/downLoad/', zipf)

Upvotes: 3

Related Questions