Home > Software engineering >  How to catch any exceptions during batch file execution of a python script?
How to catch any exceptions during batch file execution of a python script?

Time:05-23

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"

CodePudding user response:

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

  • Related