2

I am trying to produce a script that populates several fields within various feature classes I select. I am currently trying to build a list of feature classes from table of content items in an mxd. Then, I plan on using this list as input to a function (Populate) that populates the values for standardized fields.

The feature classes whose fields I will need to populate may not be gathered in a single workspace. And I may not want to perform this function on all feature classes within a particular workspace.

It seems a variable number of arguments may be necessary and that is what I have tried to perform with script similar to that shown below. The script below results in a runtime error: IOError: "[u'NameOfAFeatureClass']" does not exist. Comments in script identify some of my attempts to debug. I am running this script in the ArcMap Python window. I need to determine why the Populate function is not finding the feature classes.

mxd = arcpy.mapping.MapDocument("CURRENT")
for lyr in arcpy.mapping.ListLayers(mxd):
    if lyr.supports("DATASOURCE"):
        mylist = []
        mylist.append(lyr.name) # also tried mylist.append(lyr)

''' I tried creating a function too: 
def MxdFCs():
    mxd = arcpy.mapping.MapDocument("CURRENT")
    for lyr in arcpy.mapping.ListLayers(mxd):
        if lyr.supports("DATASOURCE"):
            mylist = []
            mylist.append(lyr)
            return mylist
MxdFCs()
'''

def Populate(*args):

    FieldOne = arcpy.ListFields(args, 'FieldOne')
    for field in FieldOne:
        if field.name == 'FieldOne':
            arcpy.CalculateField_management(args, 'FieldOne',
                                            expression='999',
                                            expression_type='PYTHON_9.3')

    FieldTwo = arcpy.ListFields(args, 'FieldTwo')
    for field in FieldOne:
        if field.name == 'FieldTwo':
            arcpy.CalculateField_management(args, 'FieldTwo',
                                            expression='999',
                                            expression_type='PYTHON_9.3')

Populate(*mylist)

1 Answer 1

0

You are passing your list of layers to your function, but are not looping through that list of layers anywhere. You either need to loop through the list and run the function on each list, or pass the list to your function and loop through the list in your function.

mxd = arcpy.mapping.MapDocument("CURRENT")
for lyr in arcpy.mapping.ListLayers(mxd):
    if lyr.supports("DATASOURCE"):
        mylist = []
        mylist.append(lyr.name) # also tried mylist.append(lyr)

def populate(fc):
    FieldOne = arcpy.ListFields(fc, 'FieldOne')
    for field in FieldOne:
        if field.name == 'FieldOne':
            arcpy.CalculateField_management(fc, 'FieldOne', expression='999', expression_type='PYTHON_9.3')

    FieldTwo = arcpy.ListFields(fc, 'FieldTwo')
    for field in FieldOne:
        if field.name == 'FieldTwo':
            arcpy.CalculateField_management(fc, 'FieldTwo', expression='999', expression_type='PYTHON_9.3')

for fc in mylist:
    populate(fc)
1
  • That answered my question. Thanks. Now I receive no errors when I run my script, however, some of the field calculations are not being performed (involving stings, integers in SDE). And for some feature classes, no calculations are being performed at all. Perhaps this is suited to another question.
    – reevesii
    May 3, 2017 at 14:52

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.