2

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?

Dan
  • 229
  • 2
  • 6
  • You shouldn't have to do any escaping for spaces and you shouldn't be adding quotes, `subprocess` will handle that for you if you give it a list of args. If you're using environment variables and want shell expansion, you'll need to set `shell=True`. If you want total control over the command and spacing, you can escape and organize the command yourself and pass a string as the `args` parameter instead of a list of arguments; it's generally best to use `shell=True` in that case – Brendan Abel Sep 14 '20 at 15:20
  • 1
    Why are the `program` and `script` values enclosed in double-quotes? I don't mean the outer set of double-quotes, but rather the inner set that are escaped with a backslash at the beginning and end of each string. These double quotes will be passed to `subproccess-run`. You don't want that. – CryptoFool Sep 14 '20 at 15:22
  • @Steve When I try without... `programm = "C:\Program Files\folder prog\folder\program.exe"` I get a... `SyntaxError: (unicode error) 'unicodeescape'... can't decode bytes in position...`... EDIT - Sorry @Steve it does actually work without the escaped chars!... the error seems to be in my other script path – Dan Sep 14 '20 at 15:27
  • 2
    @Dan you could try using forward slashes instead of back slashes – theEpsilon Sep 14 '20 at 15:29
  • @Steve I think I realised the script path error without escape characters is from the `\U` ? How would I correctly pass through a path like that? – Dan Sep 14 '20 at 15:35
  • @Dan, that error about unicode decoding shouldn't have anything to do with if you quote something or if you put in backslashes to escape something or not. You've got something else going on. Maybe you have non-ASCII characters in your commands or code? Can't you use forward slashes instead of backslashes in Windows these days? I'm a Mac/Linux guy, so I'm not all that helpful with Windows specific things. I still think it's strange that you are getting an error about unicode decoding if all of your strings are simple ASCII. – CryptoFool Sep 14 '20 at 15:38
  • @Steve Hmm, ok I'll look into it. But are the rest of the `subprocess` commands correct for what I'm trying to achieve? ie. send a cmd line `program -r script` – Dan Sep 14 '20 at 22:19
  • UPDATE... upon investigation the `unicodeerror` error was occuring from the `\U` in the `C:\User\path\to script\script.txt` path... but had a simple solution add `r` to convert it to a raw string https://stackoverflow.com/questions/1347791/unicode-error-unicodeescape-codec-cant-decode-bytes-cannot-open-text-file – Dan Sep 15 '20 at 01:35

0 Answers0