How to use subprocess.run with correct path on windows?

Question:

On windows 10 using python 3.10.10 I am trying to run a windows executable, but there is some issue with the path to the folder of the executable. I am trying to run the following code

path =  r"C:/Program Files (x86)/STMicroelectronics/STM32Cube/STM32CubeProgrammer/bin"
result = subprocess.run(command, cwd=path)

but I get the error

FileNotFoundError: [WinError 2] The system cannot find the file specified

(with and without the r).
I guess I have to "convert" the path somehow? But how exactly?

Also, the path DOES exist, and I am using similar path expressions (with the slash (/)) for other windows paths in the same python application running on windows.

Asked By: Alex

||

Answers:

According to the warning in the documentation here, windows cannot overwrite the current working directory unless you supply shell=True. Therefore, your method call would look like this:
result = subprocess.run(command, cwd=path, shell=True)

Answered By: white
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.