zoukankan      html  css  js  c++  java
  • 通过zipfile解压指定目录下的zip文件

    代码:

    # -*- 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()
  • 相关阅读:
    Domain Model
    linux 后台运行命令
    morphia(3)-查询
    [八省联考2018] 劈配
    [BZOJ 3218] a+b Problem
    [学习笔记] KM算法
    [HNOI2013] 消毒
    [HNOI2014] 画框
    [HDU 6057] Kanade's convolution
    [模板] 任意模数多项式乘法
  • 原文地址:https://www.cnblogs.com/hupeng1234/p/7247512.html
Copyright © 2011-2022 走看看