【问题标题】:UnRARing files in Python in OSX在 OSX 中用 Python 解压缩文件
【发布时间】: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 之前将目录更改为存档目录。

标签: python macos unrar


【解决方案1】:

您可以在此处使用rarfile 库:

安装:

$ pip install rarfile

示例:

from rarfile import RarFile

with RarFile("myarchive.rar") as rf:
    for f in rf.infolist():
        with open(f.filename, "wb") as of:
            of.write(rf.read(f))

更新: 或者,您可以通过以下操作一步“提取所有”:

from rarfile import RarFile


with RarFile("myarchive.rar") as rf:
    rf.extractall()

【讨论】:

  • 没问题!看起来像一个不错的小库/包装器 :) 如果您认为它可以接受并且有效,请接受它!
  • 玩弄它,我想我发现了一个稍微简单的结构:from rarfile import RarFile with RarFile("myarchive.rar") as rf: rf.extracall()
  • 我想这取决于您是否想在写出每个文件之前对每个文件进行任何处理,或者重命名它们或其他什么:)
猜你喜欢
  • 2011-09-01
  • 2011-03-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-10-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多