• Python批量解压.zip,7z文件


    import zipfile
    import py7zr
    import os
    
    def uncompress(path_name):
        suffix = path_name.rsplit('.', 1)[1]
        if suffix == 'zip':
            if zipfile.is_zipfile(path_name):
                try:
                    with zipfile.ZipFile(path_name) as zip_f:
                        zip_f.extractall(path_name.rsplit(".zip")[0])
                except Exception as e:
                    print('Error when uncompress file! info: ', e)
                    return False
                else:
                    return True
            else:
                print('This is not a true zip file!')
                return False
        if suffix == '7z':
            if py7zr.is_7zfile(path_name):
                try:
    #d_name为特殊处理的密码,文件名字的一部分 d_name
    = path_name.split(" ")[1].split(".")[0] with py7zr.SevenZipFile(path_name,password=d_name, mode='r') as sevenZ_f: sevenZ_f.extractall(path_name.rsplit(".7z")[0]) except Exception as e: print('Error when uncompress file! info: ', e) return False else: return True else: print('This is not a true 7z file!') return False # 压缩包来源目录 if __name__ == '__main__': folder_name = input("请输入文件夹--例如--E:\\workspace\\unzipTest:") os.chdir(folder_name) files = os.listdir(folder_name) for f in files: f_path = folder_name + os.sep + f if os.path.isfile(f_path): print("解压--"+f) uncompress(path_name=f_path)

     引用

    --https://www.pythonf.cn/read/143728

  • 相关阅读:
    表单实现仿淘宝搜索应用
    ASCII字符集
    HTTP状态码
    总结get和post区别
    总结Canvas和SVG的区别
    展示github中的页面(Github Pages)
    canvas的beginPath和closePath分析总结,包括多段弧的情况
    12. Integer to Roman
    13. Roman to Integer
    463. Island Perimeter
  • 原文地址:https://www.cnblogs.com/onexixi/p/14260172.html
Copyright © 2020-2023  润新知