0

I currently am using the code below to create a List of existing Domains on a Geodatabase, and if there are none to create one. The code does create the domain, but is not creating the list of Domains if they already exist. Did I set up the if statement incorrectly or do I need another function to give me the list of current domains?

I have referenced the following question, How to check if domain already exists? but the print function is not working for me.

domains=arcpy.da.ListDomains(outputGDB)
if len(domains) == 0:
    domName = 'DomStatus'
    arcpy.CreateDomain_management(r'X:\GIS_PROJECTS\City.gdb', domName, '', 'TEXT', 'CODED')
    arcpy.AddCodedValueToDomain_management(r'X:\GIS_PROJECTS\City.gdb', domName, 1, 'Boundary Used')
    arcpy.AddCodedValueToDomain_management(r'X:\GIS_PROJECTS\NATIONAL\ParkServe\Documentation\Test_HW\ParkScore_Loading\New_Mexico__Albuquerque.gdb', domName, 2, 'Added')
    arcpy.AddCodedValueToDomain_management(r'X:\GIS_PROJECTS\City.gdb', domName, 3, 'Deleted')
    arcpy.AddField_management(newFC, 'Status_2017', 'TEXT',field_length=50, field_domain=domName)
    print "Domain Created"
else:
    print domains
  • What happens if you just do your first line and your last line - domains=arcpy.da.ListDomains(outputGDB) and then on the next line print domains? It should print a number of domain objects although not specifying names. If it's not then there may be something else not working – Midavalo May 09 '17 at 18:52

2 Answers2

3

The ListDomains function returns a list of domain objects, which have a .name property.

To create a list of domain names, you have to call the name property of each domain object returned by the function. You should do something like this:

domains = arcpy.da.ListDomains(outputGDB)
domainnames = []
for domain in domains:
    domainnames.append(domain.name)
print domainnames

This will append each domain's name property to a new list (domainnames) and print it

atxgis
  • 1,239
  • 6
  • 12
3

You are getting a list of domain objects from the line domains = arcpy.da.ListDomains(outputGDB), so you will need to loop through that list and ask for the name of each domain:

domains = arcpy.da.ListDomains(outputGDB)
if len(domains) == 0:
    ...
else:
    for d in domains:
        print d.name

If this is being run through ArcMap script tool rather than in a stand-alone python script you will need to use arcpy.AddMessage() rather than print

domains = arcpy.da.ListDomains(outputGDB)
if len(domains) == 0:
    ...
else:
    for d in domains:
        arcpy.AddMessage(d.name)
Midavalo
  • 29,696
  • 10
  • 48
  • 104