You can download this code by clicking the button below.
This code is now available for download.
os module for folder and file traversal, zipfile module for creating and extracting zip files
Technology Stack : Packages and technologies used [English]
Code Type : Function
Code Difficulty :
def zipfile_create_and_extract(zip_path, extract_path):
import zipfile
import os
# 创建一个zip文件
with zipfile.ZipFile(zip_path, 'w') as zipf:
# 假设有一个文件夹名为'files',我们将它添加到zip文件中
for foldername, subfolders, filenames in os.walk('files'):
for filename in filenames:
file_path = os.path.join(foldername, filename)
zipf.write(file_path, os.path.basename(file_path))
# 从zip文件中解压文件到指定目录
with zipfile.ZipFile(zip_path, 'r') as zipf:
zipf.extractall(extract_path)