2

I'd like to execute with subprocess.Popen() this command containing an ampersand to be interpreted as a batch concatenation operator:

COMMAND="c:\p\a.exe & python run.py"
subprocess.Popen(COMMAND,cwd=wd,shell=False)`

The ampersand & however is interpreted as an argument of a.exe and not as a batch operator.

Solution 1: Having seen the related question, a solution could be to set shell=True but that gives the error 'UNC path are not supported', since my working directory is remote. This solution does not work as it is.

Solution2: The executable a.exe should take a -inputDir parameter to specify the remote location of the files and use a local working directory. I think this solution could work but I may not have the source code of the executable.

Solution3: I could instead write c:\p\a.exe & python run.py into command.bat and then use

COMMAND="c:\p\command.bat"
subprocess.Popen(COMMAND,cwd=wd,shell=False)`

Could this approach work?

Solution4: I am trying to solve this changing only the subprocess.Popen() call. Is it possible to do it? Based on the python Popen doc I suspect is not possible. Please tell me I am wrong.

See also this related questions.

UPDATE:

Solution 5: @mata suggested to use Powershell Popen(['powershell', '-Command', r'C:\p\a.exe; python run.py']). This actually works, but now I have to deal with slightly different commands, and being lazy I've decided to use solution 3.

My favourite solution was Solution 3, to create a .bat file and call it

COMMAND="c:\p\command.bat"
subprocess.Popen(COMMAND,cwd=wd,shell=False)
Community
  • 1
  • 1
Lorenzo Belli
  • 1,767
  • 4
  • 25
  • 46
  • Have you considered checking the return code yourself? – Ignacio Vazquez-Abrams Sep 29 '16 at 08:00
  • @IgnacioVazquez-Abrams I don't see how checking return code helps here. I don't need to know `a.exe` return code. – Lorenzo Belli Sep 29 '16 at 08:19
  • I think Ignacio ment to run one command, then when that finishes run the other, so use two Popen calls instead of one, otherwise you're limited by `cmd` not supporting unc paths. You could use powershell if that's available: `Popen(['powershell', '-Command', r'C:\p\a.exe; python run.py'])` instead. – mata Sep 29 '16 at 09:29
  • Thanks @mata its a good suggestion. I've tried it and almost works, but `powershell -command` then splits the arguments in a different way, I'm still working on it. – Lorenzo Belli Sep 29 '16 at 11:05
  • Are you saying that the solution #1: `call(r'c:\p\a.exe & python run.py', cwd=wd, shell=True)` causes the UNC error but the solution #3: `call(r'c:\p\command.bat', cwd=wd)` which executes the same command but from the batch-file works? Have you tried the obvious: `call(r'c:\p\a.exe', cwd=wd); call('python run.py', cwd=wd)` instead? – jfs Oct 29 '16 at 10:48
  • Hint: where the code says `shell = False`, what effect do you suppose that has? – Karl Knechtel Feb 02 '23 at 02:28

1 Answers1

0

I would use Solution 3

The & character is used to separate multiple commands on one command line. 
Cmd.exe runs the first command, and then the second command.

In this case you could just write your batch file like this:

@echo off
c:\p\a.exe
python run.py

Also, it's worth noting when using cmd.exe:

The ampersand (&), pipe (|), and parentheses ( ) are special characters
that must be preceded by the escape character (^) or quotation marks
when you pass them as arguments.
jftuga
  • 1,913
  • 5
  • 26
  • 49
  • 1
    I've tried all these approaches and this seems the more reasonable. Using `powershell -Command` create problem due to the different way it splits the parameters and due to the fact that I couldn't ignore interactive prompt boxes with it. Passing a `.bat` is now my favourite, since I can also keep `Shell=False` – Lorenzo Belli Sep 29 '16 at 16:08