2016-09-06 64 views
0

所以最近我在这里做了一个线程需要帮助的脚本,应该自动为我提取.rar文件和.zip文件,没有用户交互。随着人们的各种帮助我做出这样的:(Python)问题与Linux命令unrar,不能为我的生活找出为什么

import os 
import re 
from subprocess import check_call 
from os.path import join 

rx = '(.*zip$)|(.*rar$)|(.*r00$)' 
path = "/mnt/externa/Torrents/completed/test" 

for root, dirs, files in os.walk(path): 
    if not any(f.endswith(".mkv") for f in files): 
     found_r = False 
     for file in files: 
      pth = join(root, file) 
      try: 
       if file.endswith(".zip"): 
        print("Unzipping ",file, "...") 
        check_call(["unzip", pth, "-d", root]) 
        found_zip = True 
       elif not found_r and file.endswith((".rar",".r00")): 
        check_call(["unrar","e","-o-", pth, root]) 
        found_r = True 
        break 
      except ValueError: 
       print ("OOps! That did not work") 

我第一次运行.rar文件这个脚本它工作惊人,它提取文件到正确的目录和一切,但如果我再次运行它打印错误:

Extracting from /mnt/externa/Torrents/completed/test/A.Film/Subs/A.Film.subs.rar 

No files to extract 
Traceback (most recent call last): 
    File "unrarscript.py", line 20, in <module> 
    check_call(["unrar","e","-o-", pth, root]) 
    File "/usr/lib/python2.7/subprocess.py", line 541, in check_call 
    raise CalledProcessError(retcode, cmd) 
subprocess.CalledProcessError: Command '['unrar', 'e', '-o-', '/mnt/externa/Torrents/completed/test/A.Film/Subs/A.Film.subs.rar', '/mnt/externa/Torrents/completed/test/A.Film/Subs']' returned non-zero exit status 10 

所以我试图用一个try /除外,但我不认为我这样做是正确的,任何人都可以对这个剧本收尾帮助吗?

+1

是那里的文件? “没有文件提取”似乎unrar无法找到它 – marcadian

+0

是的,该文件在那里。 – nillenilsson

回答

0

当unrar返回一个不同于0的错误代码时,check_call引发了CalledProcessError异常。

你的错误信息显示此:

returned non-zero exit status 10

Rar.txt含有错误代码如下表:(可在WinRAR的安装文件夹中找到)

Code Description 

    0  Successful operation. 
    1  Non fatal error(s) occurred. 
    2  A fatal error occurred. 
    3  Invalid checksum. Data is damaged. 
    4  Attempt to modify an archive locked by 'k' command. 
    5  Write error. 
    6  File open error. 
    7  Wrong command line option. 
    8  Not enough memory. 
    9  File create error 
    10  No files matching the specified mask and options were found. 
    11  Wrong password. 
    255  User stopped the process. 

我看你用-o-为“跳过现有文件“。当试图覆盖文件。如果打包文件已经存在,则返回错误代码10。如果您立即重新运行您的脚本,则正常引发此错误。

C:\>unrar e -o- testfile.rar 

UNRAR 5.30 freeware  Copyright (c) 1993-2015 Alexander Roshal 


Extracting from testfile.rar 

No files to extract 

C:\>echo %errorlevel% 
10 

你或许可以做这样的事情来处理它:

except CalledProcessError as cpe: 
    if cpe.returncode == 10: 
     print("File not overwritten") 
    else: 
     print("Some other error") 

我看你尝试提取vobsubs。 vobubs rar中的.sub rar文件名也相同。

相关问题