Prevent Python Toolbox from deleting existing folder if execution fails

667
4
Jump to solution
03-20-2023 12:09 AM
Manu
by
Occasional Contributor

I have an ArcMap Python Toolbox where one input parameter is of datatype DEFolder.

When I select an existing folder for this parameter and when the tool fails, the existing folder is deleted. I think this is not desirable, since one can accidentally destroy a lot of data this way.

Is there a way to prevent this from happening? It would be nice to be able to e.g. overwrite output from previous tool execution but prevent deletion of the output folder in case of errors.

I am using ArcMap 10.8.

Here is a MWE Toolbox that reproduces the issue:

 

import arcpy as ap

class Toolbox(object):
    def __init__(self):
        """Define the toolbox (the name of the toolbox is the name of the
        .pyt file)."""
        self.label = "Toolbox"
        self.alias = ""

        # List of tool classes associated with this toolbox
        self.tools = [Tool]


class Tool(object):
    def __init__(self):
        """Define the tool (tool name is the name of the class)."""
        self.label = "Tool"
        self.description = ""
        self.canRunInBackground = False

    def getParameterInfo(self):
        """Define parameter definitions"""
        folder = ap.Parameter(
            displayName = "Output directory",
            name = "Table",
            datatype = "DEFolder",
            parameterType = "Required",
            direction = "Output")
        params = [folder]
        return params

    def isLicensed(self):
        """Set whether tool is licensed to execute."""
        return True

    def updateParameters(self, parameters):
        """Modify the values and properties of parameters before internal
        validation is performed.  This method is called whenever a parameter
        has been changed."""
        return

    def updateMessages(self, parameters):
        """Modify the messages created by internal validation for each tool
        parameter.  This method is called after internal validation."""
        return

    def execute(self, parameters, messages):
        """The source code of the tool."""
        n = 5 / 0
        return

 

 

1 Solution

Accepted Solutions
AlfredBaldenweck
MVP Regular Contributor

I think it's because you have it as an output. I switched it to Input and it fixed the problem for me.

View solution in original post

4 Replies
AlfredBaldenweck
MVP Regular Contributor

That's unusual.

Would you be able to post the code so we can exactly how it's deleting these folders?

Another question: Is it deleting all folders, or just empty ones?

0 Kudos
Manu
by
Occasional Contributor

I included a MWE in the question. In the MWE, I made the Toolbox execute part of the code fail on purpose. On my system Windows Server 2016 Standard, such a Toolbox will delete any folder that is selected in the DEFolder parameter.

0 Kudos
AlfredBaldenweck
MVP Regular Contributor

I think it's because you have it as an output. I switched it to Input and it fixed the problem for me.

Manu
by
Occasional Contributor

The idea was to create a new output folder to store output from the tool. Well, I guess this can also work by setting the variable as an input...

I guess ArcMap tries to clean up in case the tool fails. However, it does notice and show a warning when an existing folder is selected as output DEFolder. Imo ArcMap should keep track of that and not delete folders that existed prior to (attempted) tool execution.

Anyways, I have the feeling that this should not happen, even with an output. One can make entire directories disappear. I didn't try to use "C:" as output folder, though 🤣

0 Kudos