1

I'm writing this python script to recursively go through a directory and use the unrar utility on ubuntu to open all .rar files. For some reason it will enter the directory, list some contents, enter the first sub-directory, open one .rar file, then print all the other contents of the parent directory without going into them. Any ideas on why it would be doing this? I'm pretty new to python, so go easy.

"""Recursively unrars a directory"""

import subprocess
import os


def runrar(directory):
    os.chdir(directory)

    dlist = subprocess.check_output(["ls"])
    for line in dlist.splitlines():
        print(line)
        if isRar(line):
            print("unrar e " + directory + '/' + line)
            arg = directory + '/' + line
            subprocess.call(['unrar', 'e', arg])

        if os.path.isdir(line):
            print 'here'
            runrar(directory + '/' + line)


def isRar(line):
    var = line[-4:]
    if os.path.isdir(line):
        return False
    if(var == ".rar"):
        return True
    return False


directory = raw_input("Please enter the full directory: ")
print(directory)

runrar(directory)
user444381
  • 21
  • 6
  • 1
    I must say it's a pretty horrible way of scanning files... you should use `os.walk` then say goodbye to recursion. I think `os.chdir` isn't very compatible with recursive calls BTW because it introduces an unwanted side effect. – Jean-François Fabre Mar 30 '17 at 16:38

1 Answers1

1

There are a lot of problems and clumsy uses of python in your code, I may not be able to list them. Examples:

  • using os.chdir
  • using output of ls to read a directory (very wrong and not portable on windows!)
  • recursivity can be avoided with os.walk
  • computing extension of a file is easier
  • checking twice if it's a directory...

my proposal:

for root,_,the_files in os.walk(path)
    for f in the_files:
        if f.lower().endswith(".rar"):
            subprocess.call(['unrar', 'e', arg],cwd=root)

that loops through the files (and dirs, but we ignore them and we materialize it by putting the dir list into the _ variable, just a convention, but an effective one), and calls the command using subprocess which changes directory locally so unrar extracts the files in the directory where the archive is.

(In that typical os.walk loop, root is the directory and f is the filename (without path), just what we need to run a subprocess in the proper current dir, so no need for os.path.join to compute the full path)

Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219
  • Thanks for the detailed answer, like I said I'm pretty new to Python, I really appreciate your explanation of how this works – user444381 Mar 30 '17 at 16:57