Subprocess.run()找不到路径 [英] Subprocess.run() cannot find path
问题描述
我写的脚本有问题.
我只是想运行一个可执行文件(我以arduino.exe
为例).但是,我得到的是FileNotFoundError: [WinError 2]
或a non-zero exit status
(分别取决于Shell=True
是off
或on
).
I'm just trying to run an executable (I took arduino.exe
as an example). However, I either get a FileNotFoundError: [WinError 2]
or a non-zero exit status
(depending on Shell=True
is off
or on
, respectively).
我整个脚本的代码很简单:
The code of my entire script is simple:
import subprocess
subprocess.run("C:\Program Files (x86)\Arduino\arduino.exe",shell=True,check=True)
我知道Shell = True会带来安全风险,但没有找到其他方法来解决路径未找到"错误.我的另一种猜测是代码在路径中的数字和空格上挣扎了吗?
I am aware that Shell=True poses a security risk, but have found no other way to solve the path not found error. My other guess is that the code struggles with the numbers and spaces in the path?
非常感谢您的帮助.
推荐答案
您需要转义反斜杠字符.
每个反斜杠使用\\
而不是\
you need to escape backslash character.
use \\
instead of \
for every backslash,
subprocess.run("C:\\Program Files (x86)\\Arduino\\arduino.exe",shell=True,check=True)
或者您可以使用原始字符串文字
or you can use raw string literal,
subprocess.run(r"C:\Program Files (x86)\Arduino\arduino.exe",shell=True,check=True)
这篇关于Subprocess.run()找不到路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!