Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

>Solution :

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)

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading