[Biopython-dev] ApplicationResult and generic_run obsolete?

Brad Chapman chapmanb at 50mail.com
Wed Jul 8 13:06:49 UTC 2009


Hi Peter;

> I started trying to rewrite the tutorial sections using generic_run, and
> unfortunately it looks like a reasonably cross platform replacement for
> generic_run when all you want is the return code but you don't want
> the tool's output printed on screen becomes quite complex, e.g.
> 
> import subprocess
> return_code = subprocess.call(str(cline),
>                               stdin=subprocess.PIPE,
>                               stdout=subprocess.PIPE,
>                               stderr=subprocess.PIPE,
>                               shell=(sys.platform!="win32"))
> 
> We need to use pipes for stdout (and stderr) to stop the tool's output
> being printed to screen. Just using os.system(str(cline)) has the same
> problem.

How about adding a function like "run_arguments" to the commandlines
that returns the commandline as a list. It sounds like we can drop the stdin
workaround and provide a documentation item for older Windows
versions from a GUI. It might be better to use Popen and wait to
make it straightforward to learn to get stdout and stderr. So then
we get:

import subprocess
child = subprocess.Popen(cline.run_arguments(),
                         stdout=subprocess.PIPE,
                         stderr=subprocess.PIPE)
return_code = child.wait()
print child.stdout.read()

This avoids the shell nastiness with the argument list, is as simple as
it gets with subprocess, and gives users an easy path to getting stdout,
stderr and the return codes.

Also documenting how to avoid stdout and stderr entirely is useful:

import os
import subprocess
child = subprocess.Popen(cline.run_arguments(),
                         stdout=open(os.devnull, "w"),
                         stderr=subprocess.STDOUT)

Brad



More information about the Biopython-dev mailing list