python - Why does this code not back-up/ZIP directories? -
i'm making program zip files. in scenario, trying zip directory, subdirectory inside of it. i'm using following function if program has zip directory, yet doesn't zip subdirectories, takes files subdirectory , puts them others.
zipper = zipfile.zipfile(systemdate + ".zip", "w") def zipdir(path, ziph): logging.info("zip function has been called.") root, dirs, files in os.walk(path): file in files: filenom = os.path.join(root, file) print("file nom: " + filenom) zipper.write(filenom, basename(filenom))
thanks.
the second argument zipfile.write
archive name, i.e. filename of file inside archive. since zip file not contain folder information on own, that has go. in order put file inside subdirectory, have adjust arcname
include directory name.
you can use os.path.relpath
calculate path relative path
appears root of zip file:
zipper.write(filenom, os.path.relpath(filenom, path))
Comments
Post a Comment