8

I have a GIS script tool that creates a copy of an mxd, sets a definition query and exports a set of data driven pages based on the definition query. After it's finished exporting it deletes the mxd copy, some variables and a bunch of database connections.

Is there some way to code an escape code which would cancel the data driven pages exports wherever they are but instead of cancelling the whole script, it would just jump to the end and still delete the mxd copy etc. The block of code looks something like this:

    mxd = arcpy.mapping.MapDocument(mxd_master)
    mxd_folder = os.path.dirname(mxd_master)
    mxd_copy = mxd_folder + "\\WORKING.mxd"
    mxd.saveACopy(mxd_copy)
    mxd = arcpy.mapping.MapDocument(mxd_copy)

    ddp = mxd.dataDrivenPages
    for page_num in range (1, ddp.pageCount + 1):
        ddp.currentPageID = page_num
        row = ddp.pageRow
        mapsheet = row.getValue(ddp.pageNameField.name)
        arcpy.mapping.ExportToPDF(pdf export parameters....)

    del mxd, mxd_master
    arcpy.Delete_management(mxd_copy)

Basically I want to be able to hit the "Q" key (or whatever key) and the pdf export loop would break and the del/Delete_management block would still run. I don't think it's possible since it's running the script through a tool but I figured I'd see if it was as it would stop me from having to manually delete things if I decide to stop the export early.

I am using ArcGIS Desktop 10.4.

2

Take a look here.

In short your code should look like this:

 # Start of code

 try:
      #for loop
 except KeyboardInterrupt:
      pass

 #Rest of code

According to the linked Stack Overflow question Ctrl-C should work to set off KeyboardInterrupt.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.