How to get output from subprocess.Popen(). proc.stdout.readline() blocks, no data prints out

You obviously can use subprocess.communicate but I think you are looking for real time input and output. readline was blocked because the process is probably waiting on your input. You can read character by character to overcome this like the following: import subprocess import sys process = subprocess.Popen( cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE ) while True: out … Read more

Launch a shell command with in a python script, wait for the termination and return to the script

subprocess: The subprocess module allows you to spawn new processes, connect to their input/output/error pipes, and obtain their return codes. http://docs.python.org/library/subprocess.html Usage: import subprocess process = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE) process.wait() print process.returncode

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

Is it possible to run function in a subprocess without threading or writing a separate file/script.

I think you’re looking for something more like the multiprocessing module: http://docs.python.org/library/multiprocessing.html#the-process-class The subprocess module is for spawning processes and doing things with their input/output – not for running functions. Here is a multiprocessing version of your code: from multiprocessing import Process, Queue # must be a global function def my_function(q, x): q.put(x + 100) … 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

How to write to stdout AND to log file simultaneously with Popen?

You can use a pipe to read the data from the program’s stdout and write it to all the places you want: import sys import subprocess logfile = open(‘logfile’, ‘w’) proc=subprocess.Popen([‘cat’, ‘file’], stdout=subprocess.PIPE, stderr=subprocess.STDOUT) for line in proc.stdout: sys.stdout.write(line) logfile.write(line) proc.wait() UPDATE In python 3, the universal_newlines parameter controls how pipes are used. If False, … 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.