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.
Solved! Go to Solution.
It worked with the arcpy.GetParameter() function and the Data Type parameter set to String. I used the index 0. I was able to run the tool numerous times on the following files and it deleted all of them: *.zip, *.shp, *.png, *.jpeg. So:
if __name__ == "__main__":
#name of zip file with extension
oldzip = arcpy.GetParameter(0)
# output folder
workspace_folder = arcpy.GetParameterAsText(1)
# delete
delete_old_parcels_zip(oldzip, workspace_folder)
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
Thanks, that works on its own (without += operator) but embedded in the script tool with the same input as before it runs but doesn't delete and no print statements in tool.
I believe the issue lies with the script tool data type parameter for file. I'm going to concentrate on that.
Tool interface:
It worked with the arcpy.GetParameter() function and the Data Type parameter set to String. I used the index 0. I was able to run the tool numerous times on the following files and it deleted all of them: *.zip, *.shp, *.png, *.jpeg. So:
if __name__ == "__main__":
#name of zip file with extension
oldzip = arcpy.GetParameter(0)
# output folder
workspace_folder = arcpy.GetParameterAsText(1)
# delete
delete_old_parcels_zip(oldzip, workspace_folder)
I was just getting ready to indicate the parameter 0 has to be GetParameterAsText and as to be string. I tested it on my end.
Nice. Yeah, it doesn't make a difference if parameter 0 is GetParameter() or GetParameterAsText(). Both work. It's only the Data Type that needed changed.