Using && In Subprocess.popen For Command Chaining?
I'm using subprocess.Popen with Python, and I haven't come across an elegant solution for joining commands (i.e. foobar&& bizbang) via Popen. I could do this: p1 = subproce
Solution 1:
&& is a shell operator, POpen does not use a shell by default.
If you want to use shell functionality use shell=True in your POpen call, but be aware that it is slightly slower/more memory intensive.
p1 = subprocess.Popen(["mmls", "WinXP.E01", "&&", "echo", "-e", "\"\n\"", "&&", "stat", "WinXP.E01"],
stdout=subprocess.PIPE, shell=True)
Solution 2:
How about this:
from subprocess import Popen, PIPE
def log_command_outputs(commands):
processes = [Popen(cmd, stdout=PIPE) for cmd in commands]
outputs = [proc.communicate()[0].split() for proc in processes]
for output in outputs:
for line in output:
script_log.write(line)
script_long.write("\n")
This starts the commands in parallel, which might make it a little faster than doing them one by one (but probably not by a large margin). Since the communicate
calls are sequential though, any command that has a large output (more than a pipe's buffer) will block until it's turn comes to be cleaned up.
For your example command chain, you'd call:
log_command_outputs([["mmls", "WinXP.E01"], ["stat", "WinXP.E01"]])
Post a Comment for "Using && In Subprocess.popen For Command Chaining?"