'Keep the top level folder when using zipfile to zip a directory
I'm having a hard to making zipfile to retain the structure. This is the code that I am currently using:
target_dir = "/tmp/Folder"
zip = zipfile.ZipFile('/tmp/manual.zip', 'w', zipfile.ZIP_DEFLATED)
rootlen = len(target_dir) + 1
for base, dirs, files in os.walk(target_dir):
for file in files:
fn = os.path.join(base, file)
zip.write(fn, fn[rootlen:])
zip.close()
It will make a zip the file but after I extract it, the "Folder" dir will be gone and only the files inside the Folder will be received. How do I do this? What I want to happen is that when I extract the zip, it will show as 'Folder' and it should contain the files inside.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|