简体   繁体   中英

Running multiple commands using Python 3 subprocess

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.

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!

& 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM