一开始我使用了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的更多相关文章

  1. Python调用7zip命令实现文件批量解压

    Python调用7zip命令实现文件批量解压 1.输入压缩文件所在的路径 2.可以在代码中修改解压到的文件路径和所需要解压的类型,列入,解压文件夹下面所有的mp4格式的文件 3.cmd 指的就是Pyt ...

  2. python循环解压rar文件

    python循环解压rar文件 C:. │ main.py │ ├─1_STL_算法简介 │ STL_算法简介.rar │ └─2_STL_算法_填充新值 STL_算法_填充新值.rar 事情是这样的 ...

  3. java批量解压文件夹下的所有压缩文件(.rar、.zip、.gz、.tar.gz)

    // java批量解压文件夹下的所有压缩文件(.rar..zip..gz..tar.gz) 新建工具类: package com.mobile.utils; import com.github.jun ...

  4. 使用C#压缩解压rar和zip格式文件

    为了便于文件在网络中的传输和保存,通常将文件进行压缩操作,常用的压缩格式有rar.zip和7z,本文将介绍在C#中如何对这几种类型的文件进行压缩和解压,并提供一些在C#中解压缩文件的开源库. 在C#. ...

  5. linux解压rar压缩文件

    服务器是阿里云的 centos 默认linux是没有安装解压rar,zip的软件的.而网上找到的yum install命令也没有用.而且这个rar并不是免费的,只有40天的试用期...真是坑啊 由于用 ...

  6. 工作随笔——tar命令批量解压

    由于linux的tar命令不支持批量解压,所以很多网友编写了好多支持批量解压的shell命令,收集了一下,供大家分享: 第一: for tar in *.tar.gz; do tar xvf $tar ...

  7. Ubuntu 16.04 - 64bit 解压 rar 报错 Parsing Filters not supported

    Ubuntu 16.04 - 64bit  解压rar 文件报错: 错误如下图: 原因: 未安装解压命令 unrar 参考博客: Error - "Parsing Filters not s ...

  8. ubuntu 解压rar

    Ubuntu下解压rar文件的方法 一般通过默认安装的ubuntu是不能解压rar文件的,只有在安装了rar解压工具之后,才可以解压.其实在ubuntu下安装rar解压工具是非常简单的,只需要两个步骤 ...

  9. ubuntu解压rar文件

    一般通过默认安装的ubuntu是不能解压rar文件的,只有在安装了rar解压工具之后,才可以解压.其实在ubuntu下安装rar解压工具是非常简 单的,只需要两个步骤就可以迅速搞定. ubuntu 下 ...

随机推荐

  1. 【独立开发人员er Cocos2d-x实战 009】Cocos2dx 菜单项CCMenu使用

    Cocos2dx中的菜单用CCMenu/Menu类来实现.该类是一个容器.用来装载各种菜单项,用于菜单项能够是图片.系统字体等. 理论就不说了.先上代码: CCMenuItemToggle* item ...

  2. C 基础 全局变量

    /** 被static修饰的局部变量 1.只有一份内存, 只会初始化一次 2.生命周期会持续到程序结束 3.static改变了局部变量的生命周期, 但是不能改变局部变量的作用域 被static修饰的全 ...

  3. Java大数练习第二弹

    hdu1250 水题 题目链接:pid=1250">http://acm.hdu.edu.cn/showproblem.php?pid=1250 import java.util.*; ...

  4. luogu1040 加分二叉树

    题目大意 设一个n个节点的二叉树tree的中序遍历为(l,2,3,…,n),其中数字1,2,3,…,n为节点编号.每个节点都有一个分数(均为正整数),记第j个节点的分数为di,tree及它的每个子树都 ...

  5. 【http代理报文】自己开发HTTP代理工具,不限语种。

    我们知道通过修改浏览器设置可以使用代理访问网页,其实这个操作就是修改了每次的HTTP头. 工作中,我们难免需要通过TCP/IP协议发送HTTP报文来直接请求网页内容(比如爬虫工具),有同学问如何通过H ...

  6. ROADS - Roads

    N cities named with numbers 1 ... N are connected with one-way roads. Each road has two parameters a ...

  7. 分享tiny4412,emmc烧录u-boot, 支持fastboot模式烧写emmc【转】

    本文转载自:http://www.arm9home.net/read.php?tid-80810.html 分享tiny4412,emmc烧录u-boot, 支持fastboot模式烧写emmc   ...

  8. JZOJ 1003 [ 东莞市选 2007 ] 拦截导弹 —— 递推

    题目:https://jzoj.net/senior/#main/show/1003 n^2 的话递推就可以啦. 代码如下: #include<iostream> #include< ...

  9. e.printStackTrace()介绍

    public void printStackTrace()将此 throwable 及其追踪输出至标准错误流.此方法将此 Throwable 对象的堆栈跟踪输出至错误输出流,作为字段 System.e ...

  10. P1452 Beauty Contest

    传送门 求凸包周长,用旋转卡壳,具体可见yyb大佬的博客 顺便一提这题暴力+随机化也能过 暴力代码 //minamoto #include<bits/stdc++.h> #define r ...