Dynamo Python error handling help



Hi all I need a bit of help I have pounded my head against the wall trying to get this to work. I have been able to do most of what I need but I am not 100% sure if my user does not have access to write to a folder the except would be executed.

My attempt to break it did nothing my hope was that it would just write to my users My Documents folder that the except is calling but, that did not work. maybe I’m not testing it correctly.

> try:
> 	if	os.path.exists(project_dir):
> 		file_location = (project_dir)
> 	else:
> 		os.makedirs(project_dir)
> 		file_location = (project_dir)
> except OSError as exc:
> 	if exc.errno != errno.EEXIST:
> 		raise
> 	pass
> 	os.makedirs(user_dir)
> 	file_location = (user_dir)


This is a very general question. Have you tried googling specifically for some python examples. Most of the issues discussed in the forum are focused more on Dynamo & Revit than on general coding principles. You’re going to get much better answers elsewhere, like for example StackOverflow. Nevertheless, here’s a basic example on how to handle exceptions in Python:

For a more detailed error message, you’ll need to import the traceback module. You can try searching the forum for some examples on its implementation, but I’d like to stress once more that you’ll get more relevant answers on a platform like StackOverflow: https://forum.dynamobim.com/search?q=traceback


yes this is a Dyanmo python script basically its exporting differnt schedules to cvs and importing back but i need to write in some error handling if attempted folder is read only i dont want it to just error out or push a trace back. what im thinking is:

 Try: add and If not os.path...:
except OSError as exc:
      blah blah blah

Ok here is what seems to work

> try:
> 	if not os.path.exists(project_dir):
> 		os.makedirs(project_dir)
> 	file_location = (project_dir)
> except OSError as exc:
> 	if exc.errno != errno.EEXIST:
> 		raise #Reraise the current exception
> 	if os.makedirs(user_dir):
> 		file_location = (user_dir)
> else: # try everything to  keep file before complete failure
> 	print("[ERROR]  {} was inaccessible.\nWhile attempting to recover, {} did not write files not created.".format(project_dir, user_dir))