我如何添加自定义路径以保存 zip_file

how can i adding custom path for saving the zip_file

我正在使用 zipfile 模块创建一个 zip 文件。它就像一个魅力。但那是文件,保存在执行脚本的地方。 我的脚本路径是: [b]c:/User/Administrator/[/b]script.py zipfile 保存在: [b]c:/User/Administrator/[/b]backup.zip

但我想要,[b]在另一个路径[/b]中创建一个 zipfile,像这样: [b]d:/备份/[/b]backup.zip

我的代码是这样的:

import zipfile

zip_file = zipfile.ZipFile("backup.zip", 'w')
with zip_file:
    for file in filePaths:
        zip_file.write(file)

我的问题是如何添加用于保存 zip_file 的自定义路径。因为我在 C:

中没有足够的 space

tnx 很多。

给出你想要 ZipFile 函数的路径。
当你只给出 file 的名称时,它会将 file 保存在程序所在的当前目录中 运行.
改为这样做:

import zipfile
# For example you want to save it in drive 'D'
path = "D:\PathToYourDir\backup.zip"
zip_file = zipfile.ZipFile(path, 'w')
with zip_file:
    for file in filePaths:
        zip_file.write(file)