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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM