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 9 '17 at 18:52
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

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)
  • It still continues to run and not print the domains, even though there are 3 on the GDB. – Purplepeopleeater May 9 '17 at 18:42
  • @Purplepeopleeater You say in your question that it creates the domain - in that case it should never print any domain names, although it shouldn't be creating the domain if the len(domains) isn't 0 – Midavalo May 9 '17 at 18:48
  • I want to see if there are any domains associated with the GDB, checking the length to do that, so if there are 0 it will then create the one that I want and if there are existing domains to list them. Am I structuring the if statement wrong then? – Purplepeopleeater May 9 '17 at 19:25
  • @Purplepeopleeater Are you running this from a script tool in ArcMap, or stand-alone python? – Midavalo May 9 '17 at 20:02
  • 1
    @Purplepeopleeater then replace print d.name with arcpy.AddMessage(d.name) – Midavalo May 9 '17 at 20:04

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.