python压缩解压文件
2023-11-02 python 884
import zipfile # 压缩文件夹 def zip_folder(folder_path, zip_file_path): with zipfile.ZipFile(zip_file_path, 'w', zipfile.ZIP_DEFLATED) as zipf: for root, dirs, files in os.walk(folder_path): for file in files: file_path = os.path.join(root, file) zipf.write(file_path) # 解压文件夹 def unzip_folder(zip_file_path, folder_path): with zipfile.ZipFile(zip_file_path, 'r') as zipf: zipf.extractall(folder_path) # 测试代码 if __name__ == '__main__': folder_path = './example_folder' zip_file_path = './example_folder.zip' zip_folder(folder_path, zip_file_path) # 压缩文件夹 unzip_folder(zip_file_path, './unzip_folder') # 解压文件夹
很赞哦! (0)
相关文章
文章评论
-
-
-
0条评论