2016-09-06 69 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文件名也相同。

相關問題