[英]zip folder using python without changing directory
I want to zip a folder at x location to y location, I have written code for it.我想将 x 位置的文件夹 zip 到 y 位置,我已经为它编写了代码。 This is my code -这是我的代码 -
cwd = os.getcwd()
os.chdir(path.realpath(f"/y"))
shutil.make_archive(filename, "zip", file_path)
os.chdir(cwd)
I don't want to change directories, can I pass path of the folder I want to zip and path of the destination where I want to store that zipped folder?我不想更改目录,我可以将我想要的文件夹的路径传递给 zip 和我想要存储该压缩文件夹的目标路径吗? is this possible using python?这可以使用 python 吗?
If you wanted to archive /location/x/my_folder to /location/y/my_archive.zip :如果您想将/location/x/my_folder 存档到/location/y/my_archive.zip :
The base_name is describing the destination , in this case location y. base_name描述了目的地,在本例中为位置 y。
The combination of root_dir and base_dir describe the source , in this case x. root_dir和base_dir的组合描述了源,在本例中为 x。
The base_dir is the directory that gets added to the archive. base_dir是添加到存档的目录。
from shutil import make_archive
root_dir = "/location/x"
make_archive(base_name="/location/y/my_archive",
format="zip",
root_dir=root_dir,
base_dir="my_folder")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.