一开始我使用了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. Js跨一级域名同步cookie

    1. 纯Js同步两个域名下的cookie document.cookie = "name=" + "value;" + "expires=" ...

  2. word2vec 中的数学原理具体解释(六)若干源代码细节

      word2vec 是 Google 于 2013 年开源推出的一个用于获取 word vector 的工具包,它简单.高效,因此引起了非常多人的关注.因为 word2vec 的作者 Tomas M ...

  3. ios 从rgb array生成UIImage并显示,oc版

    ; ; const size_t Area = Width * Height; ; // rgba std::vector<uint8_t> output(Area*channles); ...

  4. WPF-使用面板控制内容布局,比较Canvas,WrapPanel,StackPanel,Grid,ScrollViewer

    WPF-使用面板控制内容布局,比较Canvas,WrapPanel,StackPanel,Grid,ScrollViewer 分类: WPF2012-04-24 09:59 660人阅读 评论(0)  ...

  5. 洛谷P3402 最长公共子序列

    题目背景 DJL为了避免成为一只咸鱼,来找Johann学习怎么求最长公共子序列. 题目描述 经过长时间的摸索和练习,DJL终于学会了怎么求LCS.Johann感觉DJL孺子可教,就给他布置了一个课后作 ...

  6. POJ2985 The k-th Largest Group treap

    POJ2985 比较简单的平衡树题目 树内不要添加容量为1的节点 否则会超时. #include<iostream> #include<cstdio> #include< ...

  7. JSP-Runoob:JSP JavaBean

    ylbtech-JSP-Runoob:JSP JavaBean 1.返回顶部 1. JSP JavaBean JavaBean是特殊的Java类,使用J ava语言书写,并且遵守JavaBean AP ...

  8. 72. js EXTJS grid renderer用法

    转自:https://blog.csdn.net/shancunxiaoyazhi/article/details/22156083 renderer : Function (可选的)该函数用于加工单 ...

  9. 在eclipse中如何在大量项目中查找指定文件(转载)

    转载:http://blog.csdn.net/inowcome/article/details/6699227 在eclipse中如果希望在大量的项目中寻找指定的文件可不是一件轻松的事,还好ecli ...

  10. 详细解析Linux scp命令的应用(转载)

    转自:http://os.51cto.com/art/201003/187301.htm Linux scp命令用于Linux之间复制文件和目录,具体如何使用这里好好介绍一下,从本地复制到远程.从远程 ...