python - 使用 Python 从许多 CBR 存档中删除第一个文件

标签 python rar

我有超过 400 个 CBR 文件,需要删除每个文件中包含的第一个图像,其文件名的格式为 XXX-000a.gif,其中 XXX 与包含的 CBR 文件的名称 XXX.cbr 匹配。 我将如何在 Python 中做到这一点?我使用的是 OS X Lion

最佳答案

看起来 rarfile 库不支持文件删除,所以我最终得到了以下代码:

from rarfile import RarFile, NoRarEntry
from glob import glob
import os, subprocess

CBR_DIR = "directoryWithCBRFiles"

for fname in glob(CBR_DIR + os.sep + "*.cbr"):
    toremove = os.path.basename(fname)[:-4] + "-000a.gif"
    try: # to check if the file exists
        RarFile(fname).getinfo(toremove)
    except NoRarEntry:
        print("Wanted file not found in %s." % fname)
        continue

    # rar: http://www.rarlab.com/rar/rarosx-4.2.0.tar.gz
    subprocess.call(["rar", "d", fname, toremove])

print("All done!")

关于python - 使用 Python 从许多 CBR 存档中删除第一个文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11298694/

相关文章:

python - Django-admin.py 运行服务器不工作

python - 如何使用相对路径从不同目录导入python脚本?

hash - RAR3 哈希算法

linux - 如何在 Linux 控制台中解压一个不包含一个文件的 rar 压缩包?

python - models.ForeignKey 字段未显示在可浏览的 API 中

python - Pandas 分组行值调整

cmd - 通过控制台命令将文件夹内容添加到 .rar,而不添加文件夹本身

java - 我们如何将文件从受密码保护的 RAR/ZIP 中提取到 SSD 卡中或直接提取到字符串中?

python - 使用 matplotlib 滚动浏览重叠 3D 图像的 2D 切片

python - 在 Python 中将 RAR 文件的内容读入内存