Is there any way to create an empty Popen() object, or perhaps some other way to solve what I’m trying to do.
I’ll give a code example to show what I’m trying to accomplish:
com = [["ls","/"],["wc"]]
p = Popen(["echo"])
for c in com:
p = Popen(c, stdin=p.stdout,stdout=PIPE,stderr=PIPE)
l = list(p.communicate())
I have a nested list containing system commands that I’d like to run, by iterating through the list, but for this to work p must of course exist at the start of the first iteration. I can solve this like I’ve done, by simpy issuing an echo, but I was wondering if there’s a neater and more correct way?
If all you want is to run a pipeline (= one or more |-separated commands) that is passed to your function at runtime, no need to go to all that trouble: Just use
subprocess.Pipe(pipeline, shell=True). It will handle the pipes by itself. (“pipeline” is a single string, e.g."ls"or"ls / | wc")If for some reason you really want to start these as separate pipes, your question boils down to this: You have a loop but you need an initial value to kick it off. Instead of wasting a call to Popen, I’d do it like this:
(PS. I ditched my old answer since I had misunderstood what you are doing).