这让我抓狂了,我已经查看并尝试了这里提供的一些解决此问题的答案,但到目前为止还没有什么作用。
基本问题是我有一些1300多个rar文件,我想要将它们提取出来并保持有些组织,更有趣的是,其中一些rar文件包含更多的rar文件(这就是为什么我不愿意手动操作的原因)。
我的第一次尝试,我认为我只需要编写一个简单的Python脚本,直接调用unrar即可:
这段代码返回以下内容:
“有人知道错误代码10代表什么吗?使用命令行的unrar没有任何问题。其次,我尝试使用libarchive,但尽管没有编译错误,我无法导入库。接下来我选择了pyunpack:”
这个出现了“没有这样的文件或目录”的错误。
然后我尝试了patoolib:
这个句子的意思是:这个人/物品扔掉了以下内容/物品。
尽管出现了这个信息,但当我直接从命令行运行patool时,文件可以轻松地被解压缩。
还有几根头发没被我拔掉,你有任何想法或建议吗?
基本问题是我有一些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
。因此,我猜测你要么截断了目录,要么使用了不存在的文件名。 - Roland Smith