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.

5

3 Answers 3

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!

    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.

      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.

      3
      • 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".CommentedMar 12, 2017 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 (stackoverflow.com/questions/31221279/…) might help, it's unrelated to the question at hand. You should ask another question about the cygwin issue.
        – nir0s
        CommentedMar 12, 2017 at 18:50
      • Will try. Thank you.CommentedMar 12, 2017 at 18:59

      Start asking to get answers

      Find the answer to your question by asking.

      Ask question

      Explore related questions

      See similar questions with these tags.