5

In my ArcGIS Python code, I need to loop over a couple of layers, and from those layers only select the operational fittings:

arcpy.SelectLayerByAttribute_management(fittings, "NEW_SELECTION", whereClause)

Some of my layers are stored in a personal geodatabase (.mdb) and some in a file geodatabase (.gdb), which has an impact on the syntax of the "whereClause" string, because the mdb needs square brackets for the queried field name, whereas the gdb doesn't:

if database = GDB:    # This line is pseudo-code which I need resolving
    whereClause = "LIFECYCLESTATUS = 'LIVE'"
else:
    whereClause = "[LIFECYCLESTATUS] = 'LIVE'"

My question is: how can I determine if a layer data source is in an mdb or in a gdb? I've managed to get to

desc = arcpy.Describe(fittings)
delm = desc.dataElement

But after this, I am stuck. How/Where can I find the available properties for the "dataElement" class?

1
  • i dont know how to code it right now, but if you make a variable for read the ending of the archive? you will get the .gbd or mdb and with a if you will manage it.
    – Gsanez
    Commented Jan 14, 2015 at 12:37

3 Answers 3

7

You should not bother using Describe to describe the path to the feature class first and then describing the geodatabase itself to find out whether it is personal or file one.

I recommend using the AddFieldDelimiters arcpy function which will find out the data source and use proper syntax. This means that whatever the source you will use, you will always have the correct syntax (file, personal, DBMS, shapefile).

sql_exp = """{0} = {1}""".format(
    arcpy.AddFieldDelimiters('c:/data', field_name),
    field_name)

# Use delimited field for Select tool SQL expression
arcpy.Select_analysis(in_features, out_feat_class, sql_exp)

Just in case you won't use the AddFieldDelimiters (for whatever reason), here is the code you might use otherwise. First Describe the fc path (its geodatabase in this case), then use the Workspace properties.

import arcpy
fc = r"C:\ArcGIS\pgdb.mdb\Fittings"
gdb_type = ""
desc_fc = arcpy.Describe(fc)
desc_gdb = arcpy.Describe(desc_fc.path)

if desc_gdb.workspaceFactoryProgID == 'esriDataSourcesGDB.AccessWorkspaceFactory.1':
    gdb_type = "Personal gdb"
elif desc_gdb.workspaceFactoryProgID == 'esriDataSourcesGDB.FileGDBWorkspaceFactory.1':
    gdb_type = "File gdb"
print gdb_type
6

Use the dataSource attribute for the layer.

for lyr in arcpy.mapping.ListLayers(mxd):
    dataSource = lyr.dataSource
    if ".gdb" in dataSource:                 # your pseudocode "if database = GDB:"
        print "GDB {}".format(dataSource)
    elif ".mdb" in dataSource:
        print "MDB {}".format(dataSource)
    else:
        print "OTHER"
4

Another option is to get the file extension with the python os module.

import os
gdb = "C:/filepath/mygdb.gdb"
path, ext = os.path.split(gdb)

if ext == ".gdb":
    do somethings
elif ext == ".mdb":
    do something else.

path is "C:/filepath/mygdb", ext is .gdb

1
  • This is a partial solution, but the OP would still need to get the path data from the layer before using this to determine the file extension :)
    – Erica
    Commented Jan 15, 2015 at 11:48

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.