我的代码简单如下:
file = 'C:\\Exe\\First Version\\filename.exe'
os.system(file)
当我运行这个程序时,出现一个 Windows 错误:找不到指定的文件。
我发现问题与“第一版”中间的空格有关。我怎样才能找到规避问题的方法?
附言:如果变量"file"作为参数传递给另一个函数怎么办?
最佳答案
在路径两边加上引号会起作用:
file = 'C:\\Exe\\First Version\\filename.exe'
os.system('"' + file + '"')
但更好的解决方案是改用 subprocess
模块:
import subprocess
file = 'C:\\Exe\\First Version\\filename.exe'
subprocess.call([file])
关于python - os.system 调用位于名称包含空格的目录中的 exe,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6977215/