通过zipfile解压指定目录下的zip文件
2024-08-22 06:17:56
代码:
# -*- coding: utf-8 -*-
import os
import zipfile
import platform
import multiprocessing # 解压后的文件夹与原来的zip文件同名且在相同目录下
# 确保windows下解压后的文件夹名不乱码
file_encoding = "utf-8"
if platform.system() == "Windows":
file_encoding = "gbk" # 将zip文件解压到其所在目录
def unzip(file):
file_name, ext = os.path.splitext(file)
if ext == ".zip":
print 'unzip', file
f = zipfile.ZipFile(file)
f.extractall(path=file_name.encode(file_encoding)) # 通过path指定解压的路径 if __name__ == "__main__":
path = raw_input("请输入要解压的zip文件所在目录: ")
print '要解压的zip文件所在目录为:', path
path = path.decode('utf-8') # 不同的系统和平台有各自的编码 ,为了实现系统或平台之间的信息交互可能需要编码转换
# 主要是为了兼容windows ,否则遇到中文路径 会出现 [Errno 2] # 切换到指定目录下
os.chdir(path)
file_list = os.listdir(path) # 获取指定文件夹下的文件列表
pool = multiprocessing.Pool() # 创建进程池
pool.map(unzip, file_list)
pool.close()
pool.join()
最新文章
- web前端代码编写体验
- 叠罗汉III之推箱子
- InActon-日志分析(KPI)
- 分享iOS最喜欢的技巧和提示
- 大批量DML操作应该注意什么?
- bounds的深入研究
- 初建FreeMarker工程
- Objective-C学习篇04—多态
- [LeetCode] 23. 合并K个排序链表
- Apache 80端口被占用
- CF 1110 D/E
- MyEclipse、Eclipse使用配置及部分问题
- fixed和sticky
- Linux:cut命令详解
- 数字证书签发,授权等相关以及https建立通信过程
- SQL四大语句、四大完整性、五大约束
- 《深入理解JAVA虚拟机》----------第三章 垃圾收集器与内存分配策略,笔记(下)
- Synplify9.6.2破解(转帖)
- Errors occurred during the build. Errors running builder 'DeploymentBuilder' on project
- 2015 UESTC 搜索专题B题 邱老师降临小行星 记忆化搜索