您好,登錄后才能下訂單哦!
如何在python中使用7z解壓rar文件?很多新手對此不是很清楚,為了幫助大家解決這個難題,下面小編將為大家詳細講解,有這方面需求的人可以來學習下,希望你能有所收獲。
Python是一種跨平臺的、具有解釋性、編譯性、互動性和面向對象的腳本語言,其最初的設計是用于編寫自動化腳本,隨著版本的不斷更新和新功能的添加,常用于用于開發獨立的項目和大型項目。
# coding:utf-8 import os import zipfile import traceback import time import shutil import sys,getopt succeedpath="" unsucceedpath="" psds={"2018","123456"} opts,args=getopt.getopt(sys.argv[1:],"p:") path=args[0] for psd in args[1:]: psds.add(psd) #for op,value in opts: # if op in ("-p","--password"): # psds.add(value) def mkdirs(dirpath): global succeedpath,unsucceedpath succeedpath=os.path.join(dirpath,"0.succeed") unsucceedpath=os.path.join(dirpath,"0.unsucceed") if(not os.path.exists(succeedpath)): os.makedirs(succeedpath) if(not os.path.exists(unsucceedpath)): os.makedirs(unsucceedpath) def extractdir(path): for filename in os.listdir(path): filepath=os.path.join(path,filename) if(os.path.isfile(filepath)): extractfile(filepath) def extractfile(path): (dirpath,filen)=os.path.split(path) print("解壓"+filen+" ing...") try: if(filen.endswith(".rar") or filen.endswith(".zip")): path=os.path.abspath(path) dirpath=os.path.abspath(dirpath) cmd="7z x "+path+" -o"+dirpath+" -y "+" -p" state=False if(os.system(cmd)==0): state=True else: for psd in psds: cmdp="7z x "+path+" -p"+psd+" -y "+" -o"+dirpath if(os.system(cmdp)==0): state=True break else: return if (state): shutil.move(path,succeedpath) else: shutil.move(path,unsucceedpath) with open(os.path.join(dirpath,"errorinfo.txt"),"a+") as errorinfo: errorinfo.write("time:"+str(time.time())+"\n") errorinfo.write("解壓出錯!\n可能是密碼錯誤!\n") except : with open(os.path.join(dirpath,"errorinfo.txt"),"a+") as errorinfo: errorinfo.write("time:"+str(time.time())+"\n") errorinfo.write("解壓出錯!\n") traceback.print_exc(file=errorinfo) shutil.move(path,unsucceedpath) if(os.path.isdir(path)): dirpath=path mkdirs(dirpath) extractdir(dirpath) elif(os.path.isfile(path)): filepath=path (dirpath,filename)=os.path.split(path) mkdirs(dirpath) extractfile(path) else: print("輸入錯誤!請重新輸入!")
看完上述內容是否對您有幫助呢?如果還想對相關知識有進一步的了解或閱讀更多相關文章,請關注億速云行業資訊頻道,感謝您對億速云的支持。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。