1

I have a geodatabase with multiple Feature Class (at least 100).

I would like to add in each attribute table of each Feature Class, two new columns. I have to do that with a python script because it would be very long to add the columns one by one. The two new columns are : "symbole" and "deslotID".

I try to make a python script, but I don't know anything about python. This is the code (I know it don't works)

>>> import arcpy
from arcpy import env

#My environment and the geodatabase that contains the feature class
env.workspace = "C:/data/arcgis/gdb/Featureclass.gdb"

#Set fieldname variables = 2 new columns
field_name1 = "symbole"
field_name2 = "deslotID"

#Define the fields's structure
fields = [
("featureclass", "field_name1", "LONG", 9, "", "", "refcode", "NULLABLE", "NON_REQUIRED")
("featureclass", "field_name2", "INT", 9, "", "", "refcode", "NULLABLE", "NON_REQUIRED")
]

for field in fields:
    arcpy.AddField_management(*(featureclass,) + field)

For this code, I was inspired by this question : Possible to add multiple fields in single arcpy statement?

I think I have to make a loop that runs through all the feature class in the geodatabase, I really don't know how to do that.

2

The code you require is below:

import arcpy

# Set workspace
arcpy.env.workspace = r"C:\Scratch\fGDB_Scratch.gdb"

# This returns a list of FeatureClasses in the top level of the geodatabase
fcl = arcpy.ListFeatureClasses("*")

# Main loop
for fc in fcl:
    print "Adding fields to " + fc

    # The "#" inputs mean just use defaults
    arcpy.AddField_management(fc,"symbole", "LONG", "#", "#", "#", "symbole", "NULLABLE", "NON_REQUIRED")
    arcpy.AddField_management(fc,"deslotID", "LONG", "#", "#", "#", "deslotID", "NULLABLE", "NON_REQUIRED")

print "Finished!"

I would recommend you look at the ListFeatureClasses() method to understand how you get lists of FeatureClasses. This works as the workspace environment setting for arcpy had been set.

  • Thank you for this piece of code. This seems to work, but the process is too long and makes my computer crash. It took me over 30 minutes to add two columns to two Feature Class, whereas I have over 100 Feature Class. How is this possible? Have you an idea where the problem may come ? I use ArcMap 10.3.1 @Hornbydd – Manimalis Jul 28 '15 at 9:53
  • How many rows are in these Feature Classes? – Hornbydd Jul 28 '15 at 12:27
  • Initially, before adding colums, each Feature Class have 4 columns in their table. But the columns are empty, there are 0 rows. @Hornbydd – Manimalis Jul 28 '15 at 12:33
  • May be its the fcl = arcpy.ListFeatureClasses("*") that is the rate limiting step? Just try running that to see how long it takes to create a list of featureclass name, should just be only a few seconds? – Hornbydd Jul 28 '15 at 12:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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