c popen won’t catch stderr

popen gives you a file handle on a process’ stdout, not its stderr. Its first argument is interpreted as a shell command, so you can do redirections in it: FILE *p = popen(“prog 2>&1”, “r”); or, if you don’t want the stdout at all, FILE *p = popen(“prog 2>&1 >/dev/null”, “r”); (Any other file besides … Read more

link several Popen commands with pipes

I think you want to instantiate two separate Popen objects here, one for ‘ls’ and the other for ‘sed’. You’ll want to pass the first Popen object’s stdout attribute as the stdin argument to the 2nd Popen object. Example: p1 = subprocess.Popen(‘ls …’, stdout=subprocess.PIPE) p2 = subprocess.Popen(‘sed …’, stdin=p1.stdout, stdout=subprocess.PIPE) print p2.communicate() You can keep … Read more

subprocess.Popen() error (No such file or directory) when calling command with arguments as a string

You should pass the arguments as a list (recommended): subprocess.Popen([“wc”, “-l”, “sorted_list.dat”], stdout=subprocess.PIPE) Otherwise, you need to pass shell=True if you want to use the whole “wc -l sorted_list.dat” string as a command (not recommended, can be a security hazard). subprocess.Popen(“wc -l sorted_list.dat”, shell=True, stdout=subprocess.PIPE) Read more about shell=True security issues here. In this case, … Read more

Python subprocess.Popen() wait for completion [duplicate]

Use Popen.wait: process = subprocess.Popen([“your_cmd”]…) process.wait() Or check_output, check_call which all wait for the return code depending on what you want to do and the version of python. If you are using python >= 2.7 and you don’t care about the output just use check_call. You can also use call but that will not raise … Read more

Python subprocess and user interaction

Check out the subprocess manual. You have options with subprocess to be able to redirect the stdin, stdout, and stderr of the process you’re calling to your own. from subprocess import Popen, PIPE, STDOUT p = Popen([‘grep’, ‘f’], stdout=PIPE, stdin=PIPE, stderr=STDOUT) grep_stdout = p.communicate(input=”one\ntwo\nthree\nfour\nfive\nsix\n”)[0] print grep_stdout You can also interact with a process line by … Read more

tech