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

时间:2012-07-02 17:55:06

标签: python rar

我有超过400个CBR个文件,需要删除每个文件中包含的第一个图片,其文件名的格式为XXX-000a.gif,其中XXX与包含的CBR文件的名称相匹配XXX.cbr。 我将如何在Python中执行此操作?我在 OS X Lion

2 个答案:

答案 0 :(得分:1)

看起来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!")

答案 1 :(得分:0)

没有看到你尝试过的东西,这是我能做的最好的

for fname in os.listdir('directoryWithCBRFiles'):
    if fname.endswith('-000a.gif'):
        os.remove(os.path.join('directoryWithCBRFiles', fname))

希望有所帮助