Dazzid
Dazzid

Reputation: 57

How to set a folder destination for a zip-file on python?

I'm using zipfile to compress a folder that contains different files. It works well. My only problem is that it creates the zipped file into the root folder where I'm executing the code. How can I tell the folder destination where I want the zipped file?

My code example is this one:

# Zips an entire directory using zipfile ----------------------------
def make_zipfile(_path):
    import zipfile
    if os.path.isdir(_path):
        inName = os.path.basename(_path) + '.zip'
        #head, tail = os.path.split(os.path.split(_path)[0])
        print "saving: " + inName
        def zipdir(_path, zip_handle):
            for root, dirs, files in os.walk(_path):
                for file in files:
                    print os.path.join(root, file)
                    zip_handle.write(os.path.join(root, file), file)
        with zipfile.ZipFile(inName, 'w', compression=zipfile.ZIP_DEFLATED, allowZip64=True) as z:
            zipdir(_path, z)
    print "zip file created"
    return inName

Upvotes: 1

Views: 2909

Answers (1)

Maurice Meyer
Maurice Meyer

Reputation: 18106

Use a complete path in inName:

inName = os.path.join('/tmp', os.path.basename(_path) + '.zip')

Upvotes: 2

Related Questions