You can download this code by clicking the button below.
This code is now available for download.
This function creates a zip file and compresses all files in the specified directory into the zip file.
Technology Stack : os, zipfile
Code Type : Function
Code Difficulty : Intermediate
def zipfile_create_directory(input_directory, output_zipfile):
"""
创建一个zip文件,将指定目录下的所有文件压缩到该zip文件中。
"""
import os
import zipfile
# 创建zip文件
with zipfile.ZipFile(output_zipfile, 'w') as zipf:
# 遍历目录中的所有文件
for foldername, subfolders, filenames in os.walk(input_directory):
for filename in filenames:
# 构建完整的文件路径
filepath = os.path.join(foldername, filename)
# 将文件添加到zip文件中
zipf.write(filepath, os.path.relpath(filepath, input_directory))
return output_zipfile