how to catch error with system()

Discussion in 'Python' started by eight02645999@yahoo.com, Dec 12, 2005.

  1. Guest

    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.FileNotFoundException: somefile (No such file or directory)
    at java.io.FileInputStream.open(Native Method)
    at java.io.FileInputStream.<init>(FileInputStream.java:106)
    at java.io.FileInputStream.<init>(FileInputStream.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
    , Dec 12, 2005
    #1
    1. Advertising

  2. 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
    > java.io.FileNotFoundException: somefile (No such file or directory)
    > at java.io.FileInputStream.open(Native Method)
    > at java.io.FileInputStream.<init>(FileInputStream.java:106)
    > at java.io.FileInputStream.<init>(FileInputStream.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??


    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
    Diez B. Roggisch, Dec 12, 2005
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John Black
    Replies:
    8
    Views:
    4,141
    Xenos
    Aug 20, 2004
  2. Adam
    Replies:
    9
    Views:
    553
    red floyd
    Feb 2, 2006
  3. rote
    Replies:
    4
    Views:
    1,710
    Alexey Smirnov
    Aug 4, 2008
  4. Marteno Rodia

    catch doesn't catch a thrown exception

    Marteno Rodia, Aug 3, 2009, in forum: Java
    Replies:
    5
    Views:
    549
    Daniel Pitts
    Aug 5, 2009
  5. justme

    catch error code from system()

    justme, Aug 10, 2004, in forum: Perl Misc
    Replies:
    5
    Views:
    108
    J. Romano
    Aug 10, 2004
Loading...

Share This Page