Skip to content Skip to sidebar Skip to footer

Python Zipfile Whole Path Is In File

I'm using the code from here to make a zip file from a directory. I give the function an absolute path to a folder from a GUI. Currently, say the path is c:\users......, the zip fo

Solution 1:

Do this:

def zipdir(path, zip):
    path = os.path.abspath(path)
    for root, dirs, files inos.walk(path):
        dest_dir = root.replace(os.path.dirname(path), '', 1)
        for file in files:
            zip.write(os.path.join(root, file), arcname=os.path.join(dest_dir, file))

This converts the given path to an absolute path, the directory name of which is used to remove the leading path component in root.

Post a Comment for "Python Zipfile Whole Path Is In File"