首页 > 解决方案 > 如何使用 python 归档多个文件夹?

问题描述

我正在尝试将 3 个文件夹:名为 World、World_nether、World_the_end 的文件夹压缩为 1 个 zip 文件,我只能将一个文件夹压缩:

import shutil
import zipfile

shutil.make_archive("text", 'zip', "D:\Scripts\MCserver\Server\world ",)

所有文件夹都在同一个位置/目录

D:\Scripts\MCserver\Server\world
D:\Scripts\MCserver\Server\world_nether
D:\Scripts\MCserver\Server\world_the_end

是目录

标签: pythonzipminecraftzipfile

解决方案


试试下面的代码,它将所需的文件复制到一个单独的目录中,然后压缩该目录:

import shutil

shutil.copyfile("D:\Scripts\MCserver\Server\world", "D:\Scripts\MCserver\Server\ZipFolder\world")
shutil.copyfile("D:\Scripts\MCserver\Server\world_nether", "D:\Scripts\MCserver\Server\ZipFolder\world_nether")
shutil.copyfile("D:\Scripts\MCserver\Server\world_the_end", "D:\Scripts\MCserver\Server\ZipFolder\world_the_end")

shutil.make_archive("text", 'zip', "D:\Scripts\MCserver\Server\ZipFolder")

推荐阅读