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

后端 未结 5 1842
离开以前
离开以前 2020-12-15 20:01

My code is simply as follows:

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

When I run this program, a Windows erro

5条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-15 20:22

    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])
    

提交回复
热议问题