ArcPy traverses FeatureClass in Dataset to add GLOBALID field

The feature classes of a project are stored in different datasets, and now you need to add a unique identifier field for each feature class (skip if it already exists). code show as below:

env.workspace = r'D:\RK20180201.gdb'
print env.workspace
dss = arcpy.ListDatasets ()
for ds in dss:
    fcs = arcpy.ListFeatureClasses(feature_dataset = ds)
    print fcs
    for fc in fcs:
        print(u'------current feature class : {0}'.format(fc))
        fds = arcpy.ListFields(fc)
        bGid = False
        for fd in fds:
            print(u'    -- field : {0}'.format(fd.name))
            if fd.name.upper() == 'GLOBALID':
                bGid = True
                print('    -- {0} has GID field'.format(fc))
        if False == bGid:
            print('    ++ {0} has no GID field'.format(fc))
            arcpy.AddField_management(fc,"GLOBALID","TEXT",None,None,40,"唯一标识符","NULLABLE","NON_REQUIRED")
            
Note that the FeatureClass field of the test data uses a Chinese name (it is a name, not an alias), so when printing, you need to specify the character set, that is, add u at the beginning.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325595380&siteId=291194637