I'm learning python and I'm trying to use subprocess.run
in Windows to pass a script via python to an external program using a command line argument that looks like
program -r script
.
At the moment program
is a variable defined by user input (that includes spaces) to allow for different versions. And script
is also a path that will point to the temp script path. Eg:
program = C:\Program files\folder prog\folder\program.exe
script = C:\User\path\to script\script.txt
I tried this, but it involved manual input of escape characters and only the program
argument works.
programm = "\"C:\\Program Files\\folder prog\\folder\\program.exe\""
script = "\"C:\\User\\path\\to script\\script.txt\""
process = subprocess.run([program, '-r', script],
stdout=subprocess.PIPE,
universal_newlines=True)
process
process.stdout
How do I use subprocess
correctly to run this command line argument with the correct escape characters for the variable program and script path?