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?

3
  • 1
    replace "continue" with some code that writes an error on a file? Aug 13, 2013 at 0:07
  • @GiwrgosTsopanoglou I was unsure how to do that part, the catching of the error is where I was stuck. Really simple things can sometimes be too obvious as was the case here.
    – Ryflex
    Aug 13, 2013 at 0:13
  • Yeah, I know what you mean. I think the answer rogaos gave, got you unstuck :) Aug 13, 2013 at 0:27

2 Answers 2

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()
1
  • Hmm, that's easier than I expected. Thank you ever so much, upvote and accepted your answer :)
    – Ryflex
    Aug 13, 2013 at 0: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
1
  • Interesting method of doing it, I'll use that aswell as rogoas's version.
    – Ryflex
    Aug 13, 2013 at 0:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.