I am making a python script to extract files on mass which are stored in sub folders. The whole script works fine except when I pass the command to the system using os.system()

command = '"C:\\Program Files (x86)\\unrar\\unrar.exe" x ' + '"' + ch_dir + "\\" + File + '"' + ' "' + OrigDir + '\\"'
        print "\n" + command + "\n"
        os.system(command)

print command gives me:

"C:\Program Files (x86)\unrar\unrar.exe" x "C:\Users\Core\Desktop\testing folder\LAWL\MO_to_test_success.rar" "C:\Users\Core\Desktop\testing folder\"

which when I copy and paste into the command prompt works fine and extracts said file, but when used with the os.system() gives me the following error:

'C:\Program' is not recognized as an internal or external command,
operable program or batch file.

I am sure it is something very simple however I am very new to this and I can't seem to solve it. I'm still developing that programmer mind-set :)

Thanks for any advice.

_Core

Ghar. For some reason os.system() isn't friendly with commands that have spaces in their paths (even if you enclose them with ""). Best practice is to swtich to a slightly more robust execution module such as subprocess (if you're on 2.4 or greater), or popen

Ghar. For some reason os.system() isn't friendly with commands that have spaces in their paths (even if you enclose them with ""). Best practice is to swtich to a slightly more robust execution module such as subprocess (if you're on 2.4 or greater), or popen

Hi jlm699,

Thank you for the tip, I used subprocess.call() which is working fine.

Thanks,

Hello,

I was just curious, if you got the solution to this problem as I too am in a similar fix and would like to run a pipeline of commands calling a python script.

Thanking you in advance.

I am making a python script to extract files on mass which are stored in sub folders. The whole script works fine except when I pass the command to the system using os.system()

command = '"C:\\Program Files (x86)\\unrar\\unrar.exe" x ' + '"' + ch_dir + "\\" + File + '"' + ' "' + OrigDir + '\\"'
        print "\n" + command + "\n"
        os.system(command)

print command gives me:

"C:\Program Files (x86)\unrar\unrar.exe" x "C:\Users\Core\Desktop\testing folder\LAWL\MO_to_test_success.rar" "C:\Users\Core\Desktop\testing folder\"

which when I copy and paste into the command prompt works fine and extracts said file, but when used with the os.system() gives me the following error:

'C:\Program' is not recognized as an internal or external command,
operable program or batch file.

I am sure it is something very simple however I am very new to this and I can't seem to solve it. I'm still developing that programmer mind-set :)

Thanks for any advice.

_Core

This article has been dead for over six months. Start a new discussion instead.