简体   繁体   English

zip 文件夹使用 python 而不更改目录

[英]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_dirbase_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.

相关问题 Python zip文件夹,不包括'./'(当前目录) - Python zip folder without including './' (current directory) 如何使用python提取文件夹中的所有.zip扩展名而不保留目录? - How can I extract all .zip extension in a folder without retaining directory using python? zip 包含所有内容的文件夹,但不保留 Python 中的目录结构 - zip a folder with all its content without preserving directory structure in Python 如何使用python压缩整个文件夹而不添加整个zip路径? - how to zip an entire folder using python without add entire path to zip? 提取没有文件夹python的zip文件 - extract zip file without folder python 创建没有任何目录信息的python zip - creating a python zip without any directory info Zip 多个文件夹(在目录中)到单个文件(在新目录中),在 Python - Zip multiple folder (within directory) to individual files (in a new directory), in Python Python 无路径更改目录 - Python changing directory without path 使用python将目录内的压缩文件夹中的所有文件提取到其他目录而不使用文件夹 - Extract all files from a zipped folder inside a directory to other directory without folder using python 在不改变目录的情况下写入Python中的新目录 - Writing to a new directory in Python without changing directory
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM