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 namearcname
(by default, samefilename
, without drive letter , leading path separators removed).
in case, want:
zf.write(os.path.join(root, filename), filename, zipfile.zip_deflated)