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:
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.
OK when you run it does it print your "old zip files deleted message" but nothing happens?
No, no print messages.
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?
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.
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