0

If I was to have the following bit of code:

try: 
    execfile("script.py")
except ## unsure what exception goes here...
    continue:
try: 
    execfile("other.py")
except ## unsure what exception goes here...
    continue:

How do I catch all errors from script.py save it to file and then continue onto the next called script

Anyone have any ideas or clues?

Ryflex
  • 5,559
  • 25
  • 79
  • 148

2 Answers2

2
errors = open('errors.txt', 'w')
try: 
    execfile("script.py")
except Exception as e:
    errors.write(e)
try: 
    execfile("other.py")
except Exception as e:
     errors.write(e)
errors.close()
vroomfondel
  • 3,056
  • 1
  • 21
  • 32
  • Hmm, that's easier than I expected. Thank you ever so much, upvote and accepted your answer :) – Ryflex Aug 13 '13 at 00:11
1
import traceback # This module provides a standard interface to extract, 
                 # format and print stack traces of Python programs.

try: 
    execfile("script.py")
except:
    traceback.print_exc(file=open('script.traceback.txt', 'w')) # Writing exception with traceback to file script.traceback.txt

# Here is the code that will work regardless of the success of running a script.py
mingaleg
  • 2,017
  • 16
  • 28