这让我发疯了,我在这里查看并尝试了许多解决此问题的答案,但到目前为止没有任何结果。
基本问题是我有一些 1300 多个 rar 文件,我想提取并保持一定的组织性,为了让事情变得更有趣,一些 rar 文件包含更多 rar 文件(这就是为什么我不愿意只手动执行此操作)。
我的第一次尝试,我只是想我会做一个简单的 python 脚本,直接调用 unrar:
import os
import glob
import string
import subprocess
fileCount=0
files = glob.glob('Archives/*.rar')
for file in files:
print file
callstring = ["/usr/local/bin/unrar","e",file]
output = subprocess.check_output(callstring)
print output
此代码返回以下内容:
Traceback (most recent call last):
File "/Users/Overlord/Documents/python/Unpacker.py", line 25, in <module>
output = subprocess.check_output(callstring)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 573, in check_output
raise CalledProcessError(retcode, cmd, output=output)
CalledProcessError: Command '['/usr/local/bin/unrar', 'e', 'testFile.rar']' returned non-zero exit status 10
(有人知道错误代码 10 是什么意思吗?)从命令行使用 unrar 没有任何问题。
其次,我尝试使用 libarchive,但尽管没有构建错误,但我无法导入库。
接下来我选择了pyunpack:
from pyunpack import Archive
files = glob.glob('Archives/*.rar')
for file in files:
print file
Archive(file).extractall(".")
这引发了“没有这样的文件或目录”错误。
EasyProcessError: start error <EasyProcess cmd_param=['patool', 'extract', Path(u'/Users/Overlord/Documents/python/testFile.rar'), Path(u'--outdir=/Users/Overlord/Documents/python')] cmd=['patool', 'extract', Path(u'/Users/Overlord/Documents/python/testFile.rar'), Path(u'--outdir=/Users/Overlord/Documents/python')] oserror=[Errno 2] No such file or directory returncode=None stdout="None" stderr="None" timeout=False>
然后我尝试了 patoolib:
import patoolib
files = glob.glob('Archives/*.rar')
for file in files:
print file
patoolib.extract_archive(file,outdir=".")
这个抛出了以下内容:
PatoolError: could not find an executable program to extract format rar; candidates are (rar,unrar,7z)
尽管当我直接从命令行运行 patool 时出现此消息,但该文件未解压缩,没有任何问题。
所以我回到原来的 subprocess 解决方案并尝试使用 patool 而不是 unrar
import subprocess
fileCount=0
files = glob.glob('Archives/*.rar')
for file in files:
print file
callstring = ["/Library/Frameworks/Python.framework/Versions/2.7/bin/patool","extract",file]
output = subprocess.check_output(callstring)
print output
并得到以下信息:
CalledProcessError: Command '['/Library/Frameworks/Python.framework/Versions/2.7/bin/patool', 'extract', 'testFile.rar']' returned non-zero exit status 1
当我还有几根头发没有拔掉时,有什么想法或建议吗?