python shutil 指定文件夹打包文件为 zip 压缩包,具体代码如下:
import shutil
# 指定要打包的文件夹路径
src_doc = './test'
# 指定输出的压缩包文件名(不包含扩展名)
output_filename = 'test'
fromat_ = 'zip'
# 打包并压缩文件夹为 ZIP 格式
shutil.make_archive(output_filename, fromat_, src_doc)
print(f"文件夹 {src_doc} 已成功打包为 {output_filename}.zip")
log如下:
文件夹 ./test 已成功打包为 test.zip