Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

os.system to invoke an exe which lies in a dir whose name contains whitespace

Tags:

My code is simply as follows:

file = 'C:\\Exe\\First Version\\filename.exe' os.system(file) 

When I run this program, a Windows error is raised: can't find the file specified.

I found out the problem has to with the whitespace in the middle of "First Version". How could I find a way to circumvent the problem?

P.S.: what if the variable 'file' is being passed as an argument into another function?

like image 841
Synapse Avatar asked Aug 08 '11 02:08

Synapse


2 Answers

Putting quotes around the path will work:

file = 'C:\\Exe\\First Version\\filename.exe' os.system('"' + file + '"') 

but a better solution is to use the subprocess module instead:

import subprocess file = 'C:\\Exe\\First Version\\filename.exe' subprocess.call([file]) 
like image 161
MRAB Avatar answered Oct 25 '22 16:10

MRAB


I used this:

import subprocess, shlex mycmd='"C:\\Program Files\\7-Zip\\7z" x "D:\\my archive.7z" -o"D:\\extract folder" -aou' subprocess.run(shlex.split(mycmd)) 
like image 36
abicorios Avatar answered Oct 25 '22 15:10

abicorios