syntax - How can I add only a file to zip and not the folder path leading to it in Python? -


i'm using code below zip backup file created daily.

import os, zipfile  zf = zipfile.zipfile("test.zip", "w") root, subdirs, files in os.walk("c:/users/bob/desktop/zip"):     filename in files:         zf.write(os.path.join(root, filename)) zf.close() 

the problem when open zip, includes folders in path leading file. example, inside zip, have folder called users/bob/desktop/zip/file.gdb

but want file.gdb inside zip. reason because when includes these folders, doesn't compress file. same size when it's not inside zip. if zip file.gdb 30mb 3mb.

any appreciated.

you need @ arcname parameter of zipfile.write:

zipfile.write(filename[, arcname[, compress_type]])

write file named filename archive, giving archive name arcname (by default, same filename, without drive letter , leading path separators removed).

in case, want:

zf.write(os.path.join(root, filename), filename, zipfile.zip_deflated) 

Popular posts from this blog

How to calculate SNR of signals in MATLAB? -

c# - Attempting to upload to FTP: System.Net.WebException: System error -

ios - UISlider customization: how to properly add shadow to custom knob image -