Select to view content in your preferred language

ArcPro Script Tool parameter for a variety of file types

90
5
yesterday
JaredPilbeam2
MVP Alum
import arcpy,os

# delete old zips
def delete_old_parcels_zip(oldzip, workspace_folder):
    arcpy.env.workspace = workspace_folder
    # r=root, d=directories, f=files
    for root, directories, files in os.walk(workspace_folder):
        for file in files:
            if file.endswith(oldzip): #.endswith accepts a tupule
                #delete existing zips from folder
                print("old zips deleted: {}".format(file))
                arcpy.AddMessage("old zips deleted: {}".format(file))
                arcpy.Delete_management(file)

if __name__ == "__main__":
    #name of zip file with extension
    oldzip = arcpy.GetParameterAsText(0)

    # output folder
    workspace_folder = arcpy.GetParameterAsText(1)

    # delete
    delete_old_parcels_zip(oldzip, workspace_folder)

 

I'm having trouble getting this script to work as a script tool. It works fine as a stand-alone. I've tried a number of things in Pro Catalog Toolboxes:

  1. Properties > Execution panel > embedded
  2. Properties > Execution panel > external script
  3. Properties > Parameter 0: Data Type > File > Filter File (zip; shp)
  4. Properties > Parameter 0: Data Type > Shapefile
  5. Properties > Parameter 0: Data Type > Folder
  6. Properties > Parameter 1: Data Type > Workspace

It doesn't throw any errors. But it produces no results. Basically, the tool is supposed to find a file based on user input and in the workspace and delete it. Note: I'm after learning to use if __name__ == "__main__" in a script tool. 

5 Replies
DuncanHornby
MVP Notable Contributor

OK when you run it does it print your "old zip files deleted message" but nothing happens?

0 Kudos
JaredPilbeam2
MVP Alum

No, no print messages.

0 Kudos
DuncanHornby
MVP Notable Contributor

OK what would be genuine examples of the two input parameters?

    oldzip = arcpy.GetParameterAsText(0)
    workspace_folder = arcpy.GetParameterAsText(1)

Also setting parameter zero to be a File AND  a Shapefile AND a Folder makes no sense to me, should it not simply be File with your filter set?

  1. Properties > Parameter 0: Data Type > File > Filter File (zip; shp)
  2. Properties > Parameter 0: Data Type > Shapefile
  3. Properties > Parameter 0: Data Type > Folder
JaredPilbeam2
MVP Alum

Two examples of input:

oldzip = 'WillCounty_Street.zip'
workspace_folder = r'C:\Users\jpilbeam\test'

 

Thanks for asking. So, the end-user of this tool would input files to delete in the first parameter. The files in the workspace folder could be either a *.zip, *.shp or *.pdf. What I was pointing out was the separate scenarios I've tried . As in, what I've tried was:

- setting the Data Type to File

- setting the Data Type to File, and File Filter to zip; shp

- setting the Data Type to Shapefile

- setting the Data Type to folder. --This didn't make much sense, but i tried it anyway.

 

0 Kudos
TonyAlmeida
MVP Regular Contributor

try,

for root, directories, files in os.walk(workspace_folder):
        for file in files:
            if file == oldzip or file.endswith(oldzip):
                file_path = os.path.join(root, file) #should pass the full path
                print(f"Old zip deleted: {file_path}")
                arcpy.AddMessage(f"Old zip deleted: {file_path}")
                arcpy.Delete_management(file_path)
                deleted_files += 1
0 Kudos