7

I am trying to check the existence of domains in my file geodatabase which contains 4 domains (automatically populated after I created some annotation feature classes). My code is as follows:

import arcpy

workspace = env.workspace = r"Z:\Test.gdb"

desc = arcpy.Describe(workspace)
domains = desc.domains

for domain in domains:
    if arcpy.Exists(domain):
        print domain

The issue I have is the behavior of the 'Exists' method'. When I set a conditional sentence to:

if arcpy.Exists(domain):
        print domain

the domains does not print. I'm thinking the code reads like 'If the domain exists, then print out the domain'. Because I have 4 domains, it should print these, but it doesn't.

I tried the reverse:

if not arcpy.Exists(domain):
        print domain

In this case, the domain gets printed out which seems counter intuitive. To me this reads like 'If the domain does not exists, then print the domain which should return nothing because it does not exist'

I want to test if any domains exists, instead of using a conditional statement to test if specific domains exist.

2 Answers 2

6

The domains workspace property returns a Python list. If there are no domains in the workspace, then the list is empty:

>>> desc3 = arcpy.Describe(r'D:\Temp\NcLidar.gdb')
>>> domains3 = desc3.domains
>>> domains3
[]

If you have one domain, you get a list with a single element:

>>> desc2 = arcpy.Describe(r'D:\Projects\GDBs\scratch.gdb')
>>> domains2 = desc2.domains
>>> 
>>> print domains2
[u'HU_Type Domain']

You could just check the length of the list and go from there:

>>> if len(domains3) > 0:
...     print "we have domains"
... else:
...     print "no domains"
... 
no domains

Regarding the weirdness with the Exists function, it does the same thing for me. Perhaps Exists doesn't work for domains?

>>> if not arcpy.Exists(domains3):
...     print domains3
... 
[]

>>> if  arcpy.Exists(domains3):
...     print domains3
... 
>>> 
>>> if  arcpy.Exists(domains2):
...     print domains2
... 
>>> if not arcpy.Exists(domains2):
...     print domains2
... 
[u'HU_Type Domain']

>>> 
0
3

Since you are looking at a list of domains, it follows that if there are no domains, the list will be empty when populated. Therefore, choosing the next item in the list will return none. This provides an easy method to test for an empty list.

import arcpy

workspace = env.workspace = r"Z:\Test.gdb"

desc = arcpy.Describe(workspace)
domains = desc.domains

domain = None
domain = domains.next()
if not (domain == None):
    print domain
else:
    print "List empty"
2
  • 1
    My preferred way to check whether a list is empty or not is to check its length. For example if len(domains) > 0: should work just as well.
    – blah238
    May 25, 2012 at 22:24
  • @blah238 - +1 to your comment. I'm still at the brute force stage of my pythonic coding experience. I haven't yet reached the simple yet elegant level. May 25, 2012 at 22:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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