【发布时间】:2015-08-09 01:10:34
【问题描述】:
这让我发疯了,我在这里查看并尝试了许多解决此问题的答案,但到目前为止没有任何结果。
基本问题是我有一些 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 时出现此消息,但文件未解压缩,没有任何问题。
所以我回到原来的子流程解决方案,尝试使用 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
在我还有几根头发没有拔掉的时候有什么想法或建议吗?
【问题讨论】:
-
您是否向我们展示了正确的代码?给出错误的文件名是
testFile.rar。但你的 glob 是Archives/*.rar。所以文件名应该是Archives/testFile.rar。所以我的猜测是你要么切断目录,要么使用不存在的文件名。 -
对不起,显然我是从错误的示例代码中复制粘贴过来的。但是,错误保持不变。给出错误的代码实际上在调用 unrar 之前将目录更改为存档目录。