try/exception - error block

Peter Otten __peter__ at web.de
Sun Aug 3 18:20:52 EDT 2014


bruce wrote:

> Hi.
> 
> I have a long running process, it generates calls to a separate py
> app. The py app appears to generate errors, as indicated in the
> /var/log/messages file for the abrtd daemon.. The errors are
> intermittent.
> 
> So, to quickly capture all possible exceptions/errors, I decided to
> wrap the entire "main" block of the test py func in a try/exception
> block.
> 
> This didn't work, as I'm not getting any output in the err file
> generated in the exception block.
> 
> I'm posting the test code I'm using. Pointers/comments would be
> helpful/useful.

>   try:

[...]

>   except Exception, e:
>     print e
>     print "pycolFac1 - error!! \n";
>     name=subprocess.Popen('uuidgen -t', shell=True,stdout=subprocess.PIPE)
>     name=name.communicate()[0].strip()
>     name=name.replace("-","_")
>     name2="/home/ihubuser/parseErrTest/pp_"+name+".dat"
>     ofile1=open(name2,"w+")
>     ofile1.write(e)

You can't write exceptions to the file, just strings. Try

      print >> ofile1, e

or
      ofile1.write(str(e) + "\n")

instead of the above line.

>     ofile1.write(aaa)
>     ofile1.close()
> 
>     sys.exit()




More information about the Python-list mailing list