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.

PolyGeo
65.5k29 gold badges115 silver badges350 bronze badges
asked May 9, 2012 at 18:23

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']
>>> 
answered May 9, 2012 at 19:02
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"
answered May 9, 2012 at 19:02
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. Commented 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. Commented May 25, 2012 at 22:39

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.