nillenilsson
nillenilsson

Reputation: 513

(Python) Issue with linux command unrar, cannot for the life of me figure out why

So recently i made a thread here about needing help with a script that should automatically extract .rar files and .zip files for me, without user interaction. With the various help of people i have made this:

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")

The first time i run this script on .rar files it works amazing, it extracts files to the right directory and everything but if i run it again it prints an error:

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

So i tried with a Try/except but i don't think i did it right, can anyone help on the finishing touches for this script?

Upvotes: 0

Views: 1868

Answers (1)

Gfy
Gfy

Reputation: 8329

The check_call raises the CalledProcessError exception when unrar returns an error code different from zero.

Your error message show this:

returned non-zero exit status 10

Rar.txt contains the following list of error codes: (can be found in WinRAR installation folder)

    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.

I see you use -o- to "Skip existing files." when trying to overwrite a file. If the packed file already exists, error code 10 will be returned. If you immediately rerun your script, it is normal that this error is thrown.

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

You can probably do something like this to handle it:

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

I see you try to extract vobsubs. There is also a small chance the .sub rar inside the vobubs rar has the same file name.

Upvotes: 2

Related Questions