2

I need to differentiate between two possible types of workspaces for an arcpy script. So I check the workspaceType:

rootWorkspace = raw_input('Geodatabase workspace to analyze: ')
desc = arcpy.Describe(rootWorkspace)
wsType = desc.workspaceType
print 'Workspace Type: {}'.format(wsType)

which seems to work fine and prints the correct type. But, if I try to then check the type (so I can, eventually, do different things depending on what it is):

if wsType is 'FileSystem': print 'file system'
if wsType is 'LocalDatabase': print 'gdb'
if wsType is 'RemoteDatabase': print 'sde'
else: print 'not reading this correctly'

I always get not reading this correctly (whether I'm inputting a directory or geodatabase).

According to Arc help about Workspace properties, workspaceType should be a string result (not an Object etc.), so I am confused why a string comparison isn't working.

4
  • Try using elif statements for the last 2 if's. Dec 6 '13 at 15:47
  • Just tried that, and the problem persists.
    – Erica
    Dec 6 '13 at 15:52
  • 3
    By not using elif (instead of if) your code basically said "always print not reading correctly unless wsType is RemoteDatabase". See the Python Flow Control help for more details. Dec 6 '13 at 17:20
  • @RyanDalton that corrected a secondary problem, so thank you for pointing it out -- sloppy coding on my part, I generally know better.
    – Erica
    Dec 6 '13 at 17:32
5

Try replacing "is" with "==" within the if statements. When I tested it (python 2.6) I used a local FGDB:

if wsType is 'LocalDatabase':
    print "True"
else:
    print "False"

The result here was False

if wsType =='LocalDatabase':
    print "True"
else:
    print "False"

The result here was True

The first bit of code checks if the object identity of "wsType" and 'LocalDatabase' is the same, whereas the second bit of code checks if the strings "wsType" and 'LocalDatabase' are equal.

1
  • Chose this as the answer because you not only fixed it, you told me why I'm wrong so I won't make the same mistake again :)
    – Erica
    Dec 6 '13 at 17:29
3

The following changes to your script should work for you:

import arcpy

rootWorkspace = r'C:\Users\OWNER\Documents\ArcGIS\Default.gdb'
desc = arcpy.Describe(rootWorkspace)
wsType = desc.workspaceType
print 'Workspace Type: {}'.format(wsType)

if wsType == 'FileSystem':
    print 'file system'
elif wsType == 'LocalDatabase':
    print 'gdb'
elif wsType == 'RemoteDatabase':
    print 'sde'
else:
    print 'not reading this correctly'

enter image description here

0

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.