You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
just a quick recommendation to everyone who's struggling with errors on their scripts that are not run directly instead of through the PythonConsole...
I setup all my scripts so that there's a wrapping-function in a try/except block that launches the actual code.
If the code fails, an Errorlog file is created and stored in the corresponding script folder.
Feel free to enhance it, I'm not entirely happy with the level of detail of the error messages...
"""
This is the Wrapper-script for the Function.
It stores the errorlog as a text file if the execution is crashing.
"""
# Import packages
import time
import os
import sys
import traceback
##################################################################################################
# #
# #
# 1. FUNCTION-SECTION #
# #
# #
##################################################################################################
def time2string(time_in):
"""
This function converts the time object into a readable string.
"""
y= str(curr_time.tm_year).zfill(4)
M = str(curr_time.tm_mon).zfill(2)
d = str(curr_time.tm_mday).zfill(2)
h = str(curr_time.tm_mday).zfill(2)
m = str(curr_time.tm_min).zfill(2)
s = str(curr_time.tm_sec).zfill(2)
date_str = y+' '+M+' '+d+' '+h+' '+m+' '+s+' '
return date_str
##################################################################################################
# #
# #
# 2. SCRIPT-SECTION #
# #
# #
##################################################################################################
try:
# Add the folder where this script file i stored to system paths
curr_script_folder = os.path.dirname(__file__)
sys.path.append(curr_script_folder)
######################################################
# #
# WRITE ACTUAL TO-DO SEQUENCE HERE #
# #
######################################################
# Import the actual script
import ImportAttributesFromExcel_V100 as tdo
tdo.main()
######################################################
# #
# END ACTUAL TO-DO SECTION #
# #
######################################################
# Catching runtime Exceptions
except Exception as E:
# Get the folder where this script is stored
curr_script_folder = os.path.dirname(__file__)
# Get Error Traceback
traceback_error = traceback.format_exc()
# Get current Time when error occured
curr_time = time.localtime()
timestamp = time2string(curr_time)
# Save errormessage in the folder where the script is stored
filename_errorlog = os.path.join(curr_script_folder,'Errorlog '+timestamp+'.txt')
log_file = open(filename_errorlog,'w')
log_file.write(str(traceback_error))
log_file.close()
# Open the generated file to give the user a feedback
os.startfile(filename_errorlog)
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Hi,
just a quick recommendation to everyone who's struggling with errors on their scripts that are not run directly instead of through the PythonConsole...
I setup all my scripts so that there's a wrapping-function in a try/except block that launches the actual code.
If the code fails, an Errorlog file is created and stored in the corresponding script folder.
Feel free to enhance it, I'm not entirely happy with the level of detail of the error messages...
Beta Was this translation helpful? Give feedback.
All reactions