python - os.system 调用位于名称包含空格的目录中的 exe

标签 python windowserror

我的代码简单如下:

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/

相关文章:

python - 给定一组数字,找出其中最长等差数列的长度

python - 如何修复 Flask-WTForms 中的 "The CSRF token is missing"

python - 如何按顺序检索 firebase 数据

python - 图中的最大全网格 - python 代码非常慢

python - 为 Python 安装简单音频时出现 “ERROR: Command errored out with exit status 1: python.”

Python subprocess.call - 添加一个变量到 subprocess.call

python - 更改结构化/记录数组的数据类型

python-2.7 - Windows 错误 5 : Access is denied when trying delete a directory in windows

python - 无法使用 os.remove 删除文件夹(WindowsError : [Error 5] Access is denied: 'c:/temp/New Folder' )