1

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.

Sufiyan Ghori
  • 18,164
  • 14
  • 82
  • 110
Jarn
  • 13
  • 2

1 Answers1

0

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)
Sufiyan Ghori
  • 18,164
  • 14
  • 82
  • 110