Saturday, 19 March 2016

Releasing locks to temp files after ArcPy script crashes in ArcMap?


Sometimes when I'm testing an ArcPy script and it ends with exception, ArcMap is still holding locks to some temp files (I run my scripts through ArcMap).


I cannot run the script any more, because it cannot delete/overwrite the temp files and in the end I have to restart ArcMap to release the locks.


Is there a way to do in a more clever fashion?



Answer



I've just come up with the following construction:


def main():
# my entire code


if __name__ == "__main__":
try:
main()
except Exception, e:
import gc
gc.collect()
import traceback
arcpy.AddError(traceback.format_exc())


This way I don't loose any messages and so far all locks have been released properly.


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...