(Edit: I shortened the script as a big portion of it wasn't part of the main problem.)
I'm working on a script to re-project shapefiles from a selected shapefile with a different projection. While the execution of the tool works, it fails when overwriting the same file regardless of adding in arcpy.env.overwriteOutput = True.
Here is the script used:
if srcDesc.SpatialReference.Name == targetDesc.SpatialReference.Name: continue #Shapefiles in the targeted folder that don't match will reprojected to the selected shapefile. else: #Replaces the TargetFolder's ".shp" at the end of the end of the name with "_projected." ReprojectedData = InFolder.replace(".shp", "_projected") #Tool re-projects the datasets that doesn't match the spatial reference of the targeted shapefile. arcpy.Project_management(InFolder, ReprojectedData, InFile) #Displays a message of success of the projected datasets.print "Projected " + str(InFolder) arcpy.AddMessage("Projected " + str(InFolder))
Is there a different way of using an overwrite output for a script such as this?
Solved! Go to Solution.
Is your destination workspace a file geodatabse? I ask since you are replacing *.shp with _projected, implying such.
I also suggest that you dump the try-except block and dedent everything since the exception is not informative. It is better to get a real error than a message that something failed. Exception tracking will return the same error message for the most part, as dumping the try except block.
Sometimes it is just better to use a Delete_management, to delete existing data rather than relying on the overwriteOutput
I have noticed the env overwrite is not always honored. I typically delete (as Dan suggested) or add a number to the end of my names and increment it to generate unqunie names,
I've done this many many times when "overwriting" because setting overwrite to True often fails, causing my scripts to exit,
if arcpy.Exists(fc): arcpy.management.Delete(fc)
The problem I've been seeing is when I publish a feature class to Portal with overwrite True and it creates a new service with the same name. You can't do "delete old and create new" because it will give the new layer a different unique ID and that means all your maps are broken.
(I came here because I saw Error 999999 and Overwrite in the "Related" column and got excited. :)
(I'm thinking of running off Error 999999 t-shirts, I think it would be a great inside joke for Esri users.)