0

I am working on a simple python script that will check for the existence of a shapefile, remove that shapefile if it is found, then export an SDE feature class to a shapefile. The reason for doing it this way, is if the shapefile already exists, then when the tool exports the feature class to a shapefile it will add "_1" to the name, and so on.

I am having a problem with the tool when the script identifies that the shapefile exists in the user-defined folder. If the shapefile does not exist in the folder, the tool works flawlessly, however, if it does exists, I only end up with a .shp.XML instead of the actual shapefile.

Below is the script I am using. I have removed my actual database connection filepath and replaced it with the text "SDE database connection." This script prompts the user to choose an output folder as a parameter before initiating the script.

Why am I only getting the .shp.XML file extension when the script identifies that the shapefile already exists in the user-defined folder? Should I be using a different method to check for and delete the old shapefile?

import arcpy, os

# VARIABLES
Stormwater_Points = SDE database connection
Output_Location = arcpy.GetParameterAsText(0)

# REMOVE OLD SHAPEFILE
Old_Shapefile = os.path.join(Output_Location, 'Stormwater_Points.shp')
if os.path.exists(Old_Shapefile):
    os.remove(Old_Shapefile)
else:
    arcpy.AddMessage('Old shapefile not found in this directory, exporting new shapefile to selected directory')

# EXPORT NEW SHAPEFILE
arcpy.FeatureClassToShapefile_conversion(Stormwater_Points, Output_Location)
arcpy.AddMessage('Done')

1 Answer 1

2

Instead of using os commands to check the existence of shapefiles and to delete, use the arcpy functions Exists and Delete_management

# Check for existence of data before deleting
#
if arcpy.Exists("roadbuffer"):
    arcpy.Delete_management("roadbuffer")

From: http://desktop.arcgis.com/en/arcmap/10.3/analyze/arcpy-functions/exists.htm

1
  • Simple solution, worked perfectly. Thank you for your help.
    – Jacob DA
    Sep 8, 2016 at 11:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.