Skip to content
Advertisement

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

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?

Advertisement

Answer

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])
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement