os.popen and lengthy operations

tokland at gmail.com tokland at gmail.com
Thu Sep 20 10:22:11 EDT 2007


On 20 sep, 08:31, "Dmitry Teslenko" <dtesle... at gmail.com> wrote:

> I'm using os.popen to perform lengthy operation such as building some
> project from source.

> def execute_and_save_output( command, out_file, err_file):
>         (i,o,e) = os.popen3( command )

You should consider using the higher-level "subprocess" module:

import subprocess

def run(command, output, error, bufsize=None):
    popen = subprocess.Popen(command.split(), bufsize=bufsize,
        stdout=output, stderr=error)
    popen.communicate()
    return popen.returncode

example:

create = lambda path: open(path, "w")
run("ls /etc/services abc", create("/tmp/output"), create("/tmp/
error"))

Check how to use the "bufsize" parameter in the docs:

http://docs.python.org/lib/node529.html

arnau




More information about the Python-list mailing list