0

I'm trying to run multiple commands using subprocess.Popen but I'm getting an error.

subprocess.Popen(['C:/cygwin64/Cygwin.bat' && './iv4_console.exe ../embedded/LUA/analysis/verbose-udp-example.lua'], bufsize=0, executable=None, 
                       stdin=None, stdout=None, stderr=None, 
                       preexec_fn=None, close_fds=False, 
                       shell=True, cwd="F:/Master_Copy2/iv_system4/ports/visualC12/Debug", env=None, universal_newlines=False, 
                       startupinfo=None, creationflags=0)

The error says: unsupported operand type(s) for &: 'str' and 'str' I can't figure out the problem.

Mohamed Ameen
  • 65
  • 1
  • 8

3 Answers3

1

While I am no expert on the subprocess module, I believe your problem is that you are using the windows command line command concatenation opertator && in plain python, which interprets it as &, the bitwise AND operator. You should be able to fix this by replacing

subprocess.Popen(['C:/cygwin64/Cygwin.bat' && './iv4_console.exe 
               ../embedded/LUA/analysis/verbose-udp-example.lua']...

with

subprocess.Popen(['C:/cygwin64/Cygwin.bat' + ' && ' + './iv4_console.exe 
               ../embedded/LUA/analysis/verbose-udp-example.lua']...

This replaces && with the string '&&', which then gets passed to the windows command line, which then correctly chains the commands. Hope this helps!

DW42
  • 101
  • 7
0

& is a binary operator. If you are trying to concatenate string use + instead.

Additionally, parameters for the called command should be pass as elements of the list, not in the same string.

QB_
  • 109
  • 7
0

You should use the && inside the string:

subprocess.Popen(['C:/cygwin64/Cygwin.bat && ./iv4_console.exe ../embedded/LUA/analysis/verbose-udp-example.lua'], bufsize=0, executable=None, 
                   stdin=None, stdout=None, stderr=None, 
                   preexec_fn=None, close_fds=False, 
                   shell=True, cwd="F:/Master_Copy2/iv_system4/ports/visualC12/Debug", env=None, universal_newlines=False, 
                   startupinfo=None, creationflags=0)

&& assuming you want the second command to run only if the first succeeded. Other operators (&, ;, etc..) apply depending on your requirements.

nir0s
  • 1,151
  • 7
  • 17
  • Thank you for your comment. But this didn't generate an error, but didn't work either! I'm trying to do the following: 1. Run a Cygwin terminal 2. Change dir to "F:/Master_Copy2/iv_system4/ports/visualC12/Debug". 3. Execute "./iv4_console.exe ../embedded/LUA/analysis/verbose-udp-example.lua". – Mohamed Ameen Mar 12 '17 at 18:46
  • Your question was how to run multiple commands using subprocess. This is a way to do that. I don't know why the commands aren't working for you. If you replaced the two commands you provided with two basic commands, you'll see that it works. While this (http://stackoverflow.com/questions/31221279/python-subprocess-call-using-cygwin-instead-of-cmd-on-windows) might help, it's unrelated to the question at hand. You should ask another question about the cygwin issue. – nir0s Mar 12 '17 at 18:50
  • Will try. Thank you. – Mohamed Ameen Mar 12 '17 at 18:59