Subprocess.run() cannot find path

岁酱吖の 提交于 2019-12-13 17:58:22

问题


I have a problem with a script I wrote.

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)

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?

Any help is greatly appreciated.


回答1:


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)


来源:https://stackoverflow.com/questions/53055207/subprocess-run-cannot-find-path

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!