(Python) Issue with linux command unrar, cannot for the life of me figure out why
<p>So recently i made a thread here about needing help with a script that should automatically extract <code>.rar</code> files and <code>.zip</code> files for me, without user interaction. With the various help of people i have made this:</p>

<pre><code>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")
</code></pre>

<p>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:</p>

<pre><code>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 &lt;module&gt;
    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
</code></pre>

<p>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?</p>

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

<p>Your error message show this:</p>

<blockquote>
  <p>returned non-zero exit status 10</p>
</blockquote>

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

<pre><code>    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.
</code></pre>

<p>I see you use <code>-o-</code> 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.</p>

<pre><code>C:\&gt;unrar e -o- testfile.rar

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


Extracting from testfile.rar

No files to extract

C:\&gt;echo %errorlevel%
10
</code></pre>

<p>You can probably do something like this to handle it:</p>

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

<p>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.</p>

python linux unrar