0

I have ~200 layers in a Map Document, and I am trying to find the dates that these layers were created on (so I can export to database later). Some of these layers are housed in groups and sub-groups and also in gdbs, and I think this is creating a problem when I try to extract the metadata.

# import the modules. 
import arcpy
import os
import time

# specify path of map document where layers are located. 
path = 'Location/1_STATEWIDE/'
er = path + "Tool 10_2.mxd"

# list layers using mapping module. I used this for earlier versions of the code where I had to extract layer longnames and datasources. 

mxd1 = arcpy.mapping.MapDocument(er)
layers1 = arcpy.mapping.ListLayers(mxd1)

# loop asks that for every layer in the Map Document, if the layer has a datasource, the os module will proceed to get creation time of the layer. 
The datasource condition exists so that group layers and sub-group layers will not give any results or errors; I just need times for the layers. 

for lyr in layers1:
    if lyr.supports("DATASOURCE"):
        sources = lyr.dataSource
        # need path to call time, so print list of paths before time commands.
        print os.path.getctime(sources)

This code gives the following error:

WindowsError: [Error 2] The system cannot find the file specified:     u'\\\\Location\\Terrapins_horseshoecrabs\\2002terrapindata\\terrapin_survey2002.gdb\\beach_survey_2002'

So this must be because I am trying to find the creation time for the files in a geodatabase. How can I correct this code to avoid this error?

1
  • 1
    The OS is not aware of the concept of feature classes, tables, etc. that live inside a FGDB. All it sees is a folder with a collection of files inside. If you inspect it in Windows Explorer, you won't find a file called beach_survey_2002.
    – Paul
    Apr 27 '16 at 21:50
2

That's because a geodatabase is an Esri object and not a filesystem object. You can't get a creation date of a feature class (even in ArcObjects) but you can get the date of the database that it resides in. The first thing you need to look at is lyr.isBroken, unreferenced layers are broken and trying to get the path from them will cause issues, then check lyr.isFeatureLayer before grabbing the lyr.dataSource then use a describe statement to discern the dataset types.

>>> d = arcpy.Describe (r'D:\Testing\LineLayer.shp')
>>> print d.dataType
ShapeFile
>>> d = arcpy.Describe(r'D:\Testing\x.mdb\Data\fred')
>>> print d.dataType
FeatureClass
>>> 

This will allow you discern between shapefiles and feature classes and decide what to do from there.

Read more about Layer object.

Read more about Describe.

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.