5

I have created a simple model to transfer multiple feature classes to a shapefile. With the model I exported a .py script which I'd like to run with my task manager to update any changes each day. The script that runs with out overwriting:

# Import arcpy module
import arcpy

# Local variables:
adverse = "F:\\Atco_gis\\temp_shapefiles\\LP_Layers_Temp.gdb\\adverse"
comments = "F:\\Atco_gis\\temp_shapefiles\\LP_Layers_Temp.gdb\\comments"
desig_crossing = "F:\\Atco_gis\\temp_shapefiles\\LP_Layers_Temp.gdb\\desig_crossing"
Temp_LP_Layers = "F:\\Atco_gis\\temp_shapefiles\\Temp_LP_Layers"
Temp_LP_Layers__2_ = Temp_LP_Layers

# Process: Feature Class To Shapefile (multiple)
arcpy.FeatureClassToShapefile_conversion("F:\\Atco_gis\\temp_shapefiles\\LP_Layers_Temp.gdb\\adverse;F:\\Atco_gis\\temp_shapefiles\\LP_Layers_Temp.gdb\\comments;F:\\Atco_gis\\temp_shapefiles\\LP_Layers_Temp.gdb\\desig_crossing", Temp_LP_Layers)

I have been reading that:

arcpy.env.overwriteOutput = True

should solve the problem however it leaves me with a new output with _1 or _2 depending how many times the script has been ran.

Any suggestions?

New contributor
Bella Beharrell is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
  • 4
    Welcome to GIS SE, are you putting the arcpy.env.overwriteOutput = True statement before the arcpy.FeatureClassToShapefile_conversion() statement? – artwork21 Jan 10 at 17:47
  • 1
    You could delete the old before writing the new. – Aaron Jan 10 at 18:01
  • 1
    That works in ModelBuilder, because ModelBuilder runs inside of an ArcMap session that is active where those environment variables apply. The Environment variables only work for the Python Window, not scripts. Python and arcpy scripts run without any connection to an ArcMap session, so the Environment variables of ArcMap have no effect. You should always test to see if the file exists and then delete it when it does with: if arcpy.Exists(File): arcpy.Delete_management(File) – Richard Fairhurst Jan 10 at 18:19
  • 1
    @RichardFairhurst I use the overwrite environment all the time in scripts? – Aaron Jan 10 at 20:46
  • 1
    It has never worked for me in a standalone script. After trying it on multiple occasions and having it screw up my scripts consistently, I have always used the test and delete code I mentioned. – Richard Fairhurst Jan 10 at 21:11
0

The Feature Class To Shapefile tool is a Python Script Tool and you can see its source by locating it in the Search Window and right-clicking it to choose Edit from its context menu.

It is this code, in particular a function named ConversionUtils.GenerateOutputName that appears to be generating the names with _1, _2, etc (depending how many times the script has been run) appended to them.

This behaviour is mentioned in the Feature Class To Shapefile Help and that is what bypasses your setting for arcpy.env.overwriteOutput:

If the output shapefile already exists in the Output Folder, a number will be appended to the end to make the shapefile name unique (for example, rivers_1.shp).

I would not recommend trying to change the source for this tool, and I think you will find it very difficult to try and copy the source and edit it to create a new tool.

Instead I recommend doing as @Aaron suggested in a comment and write some code, to insert before you run arcpy.FeatureClassToShapefile_conversion(), which deletes existing copies of any shapefiles before recreate them.

That code would involve using the arcpy.Exists() and arcpy.Delete_management() functions, along with some string manipulation of the pathnames to your output shapefiles.

Your Answer

Bella Beharrell is a new contributor. Be nice, and check out our Code of Conduct.

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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