python - 在 OSX 中用 Python 解压缩文件

标签 python macos unrar

这让我抓狂,我在这里查看并尝试了一些解决这个问题的答案,但到目前为止没有任何效果。

基本问题是我有一些 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

在我还有几根头发还没拔掉的时候有什么想法或建议吗?

最佳答案

您可以使用 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()

关于python - 在 OSX 中用 Python 解压缩文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30474162/

相关文章:

python - 使用 cronjob 运行带有参数的 python 脚本会出现错误 :/bin/sh: password: command not found

c++ - 有哪些用 Qt 编写的可以在 OSX 和 Windows 上运行的应用程序?

c# - 属性参数必须是常量表达式,在 C# 中是否有解决此问题的方法?

RAR 文件夹而不保留完整路径

python - __import__ 从哪里获取别名?

python - 操作系统错误 : Could not find a suitable TLS CA certificate bundle

objective-c - 从进程 ID 获取 CPU 信息

java - 拆分两个文件

c++ - 提升.Python : Getting a python weak reference to a wrapped C++ object

macos - 如何在 OS X 上自动终止进程?