how to catch error with system()

Diez B. Roggisch deets.nospaaam at
Mon Dec 12 16:27:46 CET 2005

eight02645999 at wrote:

> hi
> i have a piece of python code extract that calls an external java
> program
> cmd = """java someclass someargs"""
> try:
>    ret = os.WEXITSTATUS(os.system(cmd))
> except:
>    print blah
> else:
>    dosomething(ret)
> the thing is, the java class "someclass" produces it's own errors when
> something goes wrong.
> something like
> somefile (No such file or directory)
>         at Method)
>         at<init>(
>         at<init>(
>         ......
> how can i supress this error from showing when i execute
> ./ and at the same time logging it to an errlog file??

You probably want to catche the subprocesses stdout/stderr streams. To do
so, use the subprocess module (if you are on python2.4), or the
popen2-module. See the docs for how to use them.


Diez B. Roggisch

More information about the Python-list mailing list