如何在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进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。