Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

subprocess.Popen() IO redirect

Tags:

python

popen

Trying to redirect a subprocess' output to a file.

server.py:

while 1:
    print "Count " + str(count)
    sys.stdout.flush()
    count = count + 1
    time.sleep(1)

Laucher:

cmd = './server.py >temp.txt'
args = shlex.split(cmd)
server = subprocess.Popen( args )

The output appear on screen, temp.txt remains empty. What am I doing wrong?

As background I am trying to capture the output of a program that has already been written.

I cannot use:

server = subprocess.Popen(
                [exe_name],
                stdin=subprocess.PIPE, stdout=subprocess.PIPE)

as the program may not flush. Instead I was going to redirect output through a fifo. This works fine if I manually launch server.py but obviously not if I Popen() cause redirect doesnt work. ps -aux shows that server.py was launched correctly.

like image 895
Pete Roberts Avatar asked Jan 17 '12 21:01

Pete Roberts


People also ask

What is subprocess Popen?

The subprocess module defines one class, Popen and a few wrapper functions that use that class. The constructor for Popen takes arguments to set up the new process so the parent can communicate with it via pipes. It provides all of the functionality of the other modules and functions it replaces, and more.

What does the popen () return on success?

If successful, popen() returns a pointer to an open stream that can be used to read or write to a pipe. If unsuccessful, popen() returns a NULL pointer and sets errno to one of the following values: Error Code. Description.

What is difference between subprocess Popen and call?

Popen is more general than subprocess. call . Popen doesn't block, allowing you to interact with the process while it's running, or continue with other things in your Python program. The call to Popen returns a Popen object.

Does subprocess Popen block?

Popen is nonblocking. call and check_call are blocking. You can make the Popen instance block by calling its wait or communicate method.


2 Answers

Altenatively, you can use the stdout parameter with a file object:

with open('temp.txt', 'w') as output:
    server = subprocess.Popen('./server.py', stdout=output)
    server.communicate()

As explained in the documentation:

stdin, stdout and stderr specify the executed program’s standard input, standard output and standard error file handles, respectively. Valid values are PIPE, an existing file descriptor (a positive integer), an existing file object, and None.

like image 66
jcollado Avatar answered Sep 23 '22 00:09

jcollado


Output redirection with ">" is a feature of shells - by default, subprocess.Popen doesn't instantiate one. This should work:

server = subprocess.Popen(args, shell=True)
like image 22
AdamKG Avatar answered Sep 23 '22 00:09

AdamKG