Archive

Posts Tagged ‘subprocess’

Execute command, show its output, get its exit code

March 3, 2013 Leave a comment

Problem
You want to execute an external program, show its output, and get its exit code at the end.

Solution

import shlex
from subprocess import Popen, PIPE

cmd = "..."
process = Popen(shlex.split(cmd), stdout=PIPE)
process.communicate()    # execute it, the output goes to the stdout
exit_code = process.wait()    # when finished, get the exit code

Example: I used this code with FFmpeg. FFmpeg updates the output on the stdout regularly while doing a conversion. At the end I can ask if the conversion was successful (exit_code == 0) or not.

Categories: python Tags: , ,

Calling external commands

April 28, 2011 Leave a comment

How to call external commands in Python:

Categories: python Tags: ,
Design a site like this with WordPress.com
Get started