shutil.make_archive 没有压缩到正确的目的地

shutil.make_archive not zipping to correct destination

根据下面的代码,我在使用 python 3 shutil.make_archive 函数压缩目录时遇到问题。 .testdir 将被压缩,但它被压缩在 /home/pi,而不是 /home/pi/Backups.

zip_loc = '/home/pi/.testdir'
zip_dest = '/home/pi/Backups/'
shutil.make_archive(zip_loc, 'zip', zip_dest)

谁能解释一下我做错了什么?

阅读文档 here 我想到了:

zip_loc = '/home/pi/.testdir'
zip_dest = '/home/pi/Backups/'
shutil.make_archive(base_dir=zip_loc, root_dir=zip_loc, format='zip', base_name=zip_dest)

来自文档:

base_name is the name of the file to create, including the path, minus any format-specific extension.

root_dir is a directory that will be the root directory of the archive; for example, we typically chdir into root_dir before creating the archive.

base_dir is the directory where we start archiving from; i.e. base_dir will be the common prefix of all files and directories in the archive.

root_dir and base_dir both default to the current directory.

在写存档之前,移动到好目录:

old_path = os.getcwd()
os.chdir(path)

-> 写入存档

写入存档后移回旧目录:

os.chdir(old_path)