python利用7z批量解压rar
一开始我使用了rarfile这个库,奈何对于含有密码的压缩包支持不好,在linux上不抛出异常;之后有又尝试了unrar。。比rarfile还费劲。。
所以用了调用系统命令的方法,用7z来解压
通过apt可以安装上7z-full和7z的rar插件
有一个地方要注意 -o 和-p与后面的目录、密码之间没有空格!!
同样可以用这个代码暴力破解压缩包密码
# 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("输入错误!请重新输入!")
python利用7z批量解压rar的更多相关文章
- Python调用7zip命令实现文件批量解压
Python调用7zip命令实现文件批量解压 1.输入压缩文件所在的路径 2.可以在代码中修改解压到的文件路径和所需要解压的类型,列入,解压文件夹下面所有的mp4格式的文件 3.cmd 指的就是Pyt ...
- python循环解压rar文件
python循环解压rar文件 C:. │ main.py │ ├─1_STL_算法简介 │ STL_算法简介.rar │ └─2_STL_算法_填充新值 STL_算法_填充新值.rar 事情是这样的 ...
- java批量解压文件夹下的所有压缩文件(.rar、.zip、.gz、.tar.gz)
// java批量解压文件夹下的所有压缩文件(.rar..zip..gz..tar.gz) 新建工具类: package com.mobile.utils; import com.github.jun ...
- 使用C#压缩解压rar和zip格式文件
为了便于文件在网络中的传输和保存,通常将文件进行压缩操作,常用的压缩格式有rar.zip和7z,本文将介绍在C#中如何对这几种类型的文件进行压缩和解压,并提供一些在C#中解压缩文件的开源库. 在C#. ...
- linux解压rar压缩文件
服务器是阿里云的 centos 默认linux是没有安装解压rar,zip的软件的.而网上找到的yum install命令也没有用.而且这个rar并不是免费的,只有40天的试用期...真是坑啊 由于用 ...
- 工作随笔——tar命令批量解压
由于linux的tar命令不支持批量解压,所以很多网友编写了好多支持批量解压的shell命令,收集了一下,供大家分享: 第一: for tar in *.tar.gz; do tar xvf $tar ...
- Ubuntu 16.04 - 64bit 解压 rar 报错 Parsing Filters not supported
Ubuntu 16.04 - 64bit 解压rar 文件报错: 错误如下图: 原因: 未安装解压命令 unrar 参考博客: Error - "Parsing Filters not s ...
- ubuntu 解压rar
Ubuntu下解压rar文件的方法 一般通过默认安装的ubuntu是不能解压rar文件的,只有在安装了rar解压工具之后,才可以解压.其实在ubuntu下安装rar解压工具是非常简单的,只需要两个步骤 ...
- ubuntu解压rar文件
一般通过默认安装的ubuntu是不能解压rar文件的,只有在安装了rar解压工具之后,才可以解压.其实在ubuntu下安装rar解压工具是非常简 单的,只需要两个步骤就可以迅速搞定. ubuntu 下 ...
随机推荐
- java中不能使用小数点(.)来作为分隔符
split()括号里是一个String的参数,所以一定要符合这种:split(".")形式,即点'.'要用双引号""括起来"."在java中 ...
- ios MetalPerformanceShaders 使用总结
MPSCNNConvolution 1.初始化时传人 UnsafePointer<Float> 时要传入w,不能是 &(w[0]).否则w其实传入失败,卷积的结果是nan或inf. ...
- 3301 Square words
3301 Square words 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题解 查看运行结果 题目描述 Description 定义s ...
- ubuntu安装typora
optional, but recommended sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys BA300B7755AF ...
- Scala 获取当前时间
def NowDate(): String = { val now: Date = new Date() val dateFormat: SimpleDateFormat = new SimpleDa ...
- 利用【监听器】动态加载Log4j配置文件
转自:https://veromca273.iteye.com/blog/1889304 1 创建监听器: public class LogListener implements ServletCon ...
- 同一个Tomcat下不同项目之间的session共享
最近发现项目运行过程中经常会抛出一个 NullPointerException的异常,经检查发现异常出现的地方是日志模板,一阵检查,正常无误 (把所有记录日志的地方都点了一遍,心里是崩溃的),万念俱灰 ...
- phpci发送邮件
$config['protocol']='smtp'; $config['smtp_host']='smtp.163.com';//163服务器,之前用了qq服务器死活发不出去,不知道什么原因,可以自 ...
- ie8 js编译器对象为空或不是对象的一个小问题
昨天在遍历json串的时候碰到了如下图所示的问题,除ie8以下版本的浏览器运行都是正常的, 部分代码如下: 1 var Workmodel=function(){ 2 model_json=[ 3 { ...
- 知识记忆1:标志寄存器PSW
8086CPU的flag寄存器结构如下: [ ] [ ] [ ] [ ] [OF][DF][IF][TF][SF][ZF][ ][AF][ ][PF][ ][CF] 其中debug中的显示为: 标志 ...