Need help building my first script

Jump to solution
03-28-2016 09:19 AM
New Contributor III

Hi, I'm trying to build a script, my first, so that it would create another version of some polygons in a shapefile (per example zoning)by erasing some polygons contained in a different shapefile (called hydro). I don't have a Pro license, so I use the "Union then delete" method instead of the erase tool. I don't want the attributes from the hydro table. I want to duplicate the script to apply those modification to other shapefiles. So here is what I found for the moment:

# Import the system modules

import arcpy

from arcpy import env

# Set the current workspace

# (to avoid having to specify the full path to the feature classes each time)

env.workspace = "c:/data/data.gdb"

# For first layer

  #Turn off all fields of hydro.shp so when union is done, the attributes table will only keep the fields from "zoning", leaving out the turned off fields of "hydro"

  #Can't find script lines for that

  #union layerName with hydro.shp


  arcpy.Union_analysis (["layerName", "hydro", "layerName_union", "ALL")

  #select FID_hydro <> -1


  arcpy.SelectLayerByAttribute_management ("layerName_union", "NEW_SELECTION", " [FID_hydro] <> '-1' ")

  #If selection > 0, delete FID_hydro <> -1


  if int(arcpy.GetCount_management(layerName_union).getOutput(0)) > 0:


  #export data layerName_union as shapefile



# Repeat for other layers

Anyone know how to use script to turn off all the field of a layer?

Would those steps work? Is there something missing?


Tags (1)
0 Kudos
1 Solution

Accepted Solutions
New Contributor III
import arcpy
import os

#delete any data tha might be stored in memory

#set workspace to where all your shapefiles are
arcpy.env.workspace="C:/Users/Antoine/Desktop/MRCL - Local/Ketar_shp"

#define where the hydro shapefile is located, ideally in a different folder than the others
hydroshp="C:/Users/Antoine/Desktop/MRCL - Local/Ketar_shp/hydro/hydro.shp"

for fc in fcs:
    #create an empty list to be populated with the original shapefile's field names

    #List all fields in the shapefile and populate the fieldnames list with their names
    for field in fields:

    #define layer name for each shapefile

    #create output files for the clip and the union layers

    #clip the shapefile using the hydro layer

    #create a union of the clipped hydro and the shapefile

    #delete any unnecessary fields from your union shapefile
    for ufield in unionfields:
        if not in fieldnames:

    #Create layer file so you can perform a selection on it

    #If selection > 0, delete features
    if int(arcpy.GetCount_management(fclayer+"_sansO").getOutput(0)) > 0:

    #export data layerName_union as shapefile (works)
    arcpy.FeatureClassToFeatureClass_conversion(fclayer+"_sansO","C:/Users/Antoine/Desktop/MRCL - Local/Ketar_shp/shapes_sansO.gdb",fclayer+"_sansO")

print "done!!"

Someone helped me, here's what we have come up with!

View solution in original post

22 Replies
MVP Honored Contributor

I think you're looking for the Delete Field tool.

If you need more help, please include your code within code tags so we can copy/paste/more easily read. Posting Code blocks in the new GeoNet

MVP Esteemed Contributor

and featureclasstofeatureclass allows you to remove fields during the fieldmapping process Feature Class to Feature Class—Help | ArcGIS for Desktop

New Contributor III

Would it looks like that?

arcpy.FeatureClassToFeatureClass_conversion("Affectations_Union","C:\Users\Antoine\Desktop\test","layerName_shape.shp",,removeAll ())

0 Kudos
MVP Esteemed Contributor

according to this FieldMappings—Help | ArcGIS for Desktop in scripting, I think that would be correct, but I am not at a machine to test at present

0 Kudos
New Contributor III

Don't seem to work, I get this message:

"Parsing error SyntaxError: invalid syntax (line 1)" !?

0 Kudos
MVP Esteemed Contributor

fms = arcpy.FieldMappings()


Create the output feature class, using the FieldMappings object arcpy.FeatureClassToFeatureClass_conversion( in_file, arcpy.env.workspace, out_file, field_mapping=fms)

guess-ta-coding from the help files

MVP Esteemed Contributor

or you can't have ,, in a row, something has to be inbetween

New Contributor III

I tried this:

fms = arcpy.FieldMappings()

I get this message now:

"Runtime error  Traceback (most recent call last):   File "<string>", line 1, in <module>   File "c:\program files (x86)\arcgis\desktop10.2\arcpy\arcpy\", line 1685, in FeatureClassToFeatureClass     raise e ExecuteError: ERROR 999999: Error executing function. An invalid SQL statement was used. An invalid SQL statement was used. Failed to execute (FeatureClassToFeatureClass). "

When I try to use the FeatureClassToFeatureClass tool, in the UI, I get another error message. I tried to solve it with this solution​. Still not working.

0 Kudos
MVP Esteemed Contributor

the syntax I posted was  field_mapping=fms)

You assigned it a text value "fms" which will surely fail.  Check the exact syntax in the help files and what the defaults are if you omit parameters.  You can't just omit parameters by using ,, or changing object properties ( a fieldmap is a type of object which is not a string object).  These errors aren't caught by IDE's, the clues are in the reported message, unfortunately they are interpreted with respect to the object expected and not by what the user expect to be given to solve the problem.