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
  • why you ar useing && Commented Mar 12, 2017 at 18:35
  • Read this, String Special Operators: tutorialspoint.com/python/python_strings.htm Commented Mar 12, 2017 at 18:41
  • @AnkurJyotiPhukan I'm trying to execute the commands one by one sequentially. Commented Mar 12, 2017 at 18:50
  • then you have to include into string Commented Mar 12, 2017 at 18:55
  • 1
    I believe this has been answered twice below. Commented Mar 12, 2017 at 18:56

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!

Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

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 Comments

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".
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.
Will try. Thank you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.