how to catch error with system()

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • eight02645999@yahoo.com

    #1

    how to catch error with system()

    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
    java.io.FileNot FoundException: somefile (No such file or directory)
    at java.io.FileInp utStream.open(N ative Method)
    at java.io.FileInp utStream.<init> (FileInputStrea m.java:106)
    at java.io.FileInp utStream.<init> (FileInputStrea m.java:66)
    ......

    how can i supress this error from showing when i execute
    ../pythonscript.py and at the same time logging it to an errlog file??

    thanks

  • Diez B. Roggisch

    #2
    Re: how to catch error with system()

    eight02645999@y ahoo.com wrote:
    [color=blue]
    > 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
    > java.io.FileNot FoundException: somefile (No such file or directory)
    > at java.io.FileInp utStream.open(N ative Method)
    > at java.io.FileInp utStream.<init> (FileInputStrea m.java:106)
    > at java.io.FileInp utStream.<init> (FileInputStrea m.java:66)
    > ......
    >
    > how can i supress this error from showing when i execute
    > ./pythonscript.py and at the same time logging it to an errlog file??[/color]

    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.

    --
    Regards,

    Diez B. Roggisch

    Comment

    Working...