XYZ123
XYZ123

Reputation: 5

How to catch any exceptions during batch file execution of a python script?

I have a batch file scheduled to run a python script every morning. The script is supposed to append to a .csv file. However, some days no data is appended to the csv file which I believe is due to an error during execution of the script. Is there anything I can add to my batch file to save any error messages that cause the script to stop execution to a .txt file? Or is there a better way to do this within the python script instead? Right now in my batch file I just have

python "C:\filepath\pythonscript.py"

Upvotes: 0

Views: 636

Answers (1)

user2956477
user2956477

Reputation: 1350

Append redirection to command line, as displayed bellow:

python "C:\filepath\pythonscript.py" 2>>log.txt

2>>log.txt statement redirect STDERR (errors printed by python interpreter) to a log file in APPEND mode (single > will switch to OVERWRITE mode). If you want to capture STDOUT too, change added apendix to >>log.txt 2>&1

Upvotes: 1

Related Questions