Sakti Aishwarya
Sakti Aishwarya

Reputation: 1

Zipping to a specific directory in python

I tried zipping a directory /tmp using the following code

#!/usr/bin/env python
import os
import zipfile


def zipdir(path, zip):
    for root, dirs, files in os.walk(path):
        for file in files:
            zip.write(os.path.join(root, file))

if __name__ == '__main__':
    zip = zipfile.ZipFile('Python.zip', 'w')
    zipdir('tmp/', zip)
    zip.close()

The output of the above code seems to create the zip file in "/" directory. Is there any way to re-direct the output to another directory, say, /home/zipfiles ?

Upvotes: 0

Views: 83

Answers (1)

user2357112
user2357112

Reputation: 280181

You should be able to just set

zip = zipfile.ZipFile('/home/zipfiles/whatever.zip', 'w')

What you have now will create the file in whatever directory you're running the script from.

Note: It'd probably be a good idea to pick a name that isn't zip. zip is a builtin, and it's always annoying to find that you're getting weird TypeErrors because you accidentally shadowed a builtin you needed.

Upvotes: 1

Related Questions