0

I have parent directory containing subfolders with layer files (.lyr) and I want to list Feature Classes, Geometry Types, server and user names for these files. Also I need Python to scan these files recursively. I am only a beginner in Python, so I don't really know how to do it. All I've done so far is here:

import os, fnmatch
import arcpy

for dirpath, dirnames, filenames in os.walk("F:\\"):
    for filename in filenames:
        if fnmatch.fnmatch(filename, "*.lyr"):
            lyr_filename = os.path.join(dirpath,filename)
            layers = arcpy.mapping.MapDocument(lyr_filename)
            for layer in arcpy.mapping.ListLayers(layers):
                    print(lyr_filename,layer.serviceProperties)

This code scan all the files in the subfolders, but it does not print the information I need. Version of my ArcCatalog is 10.0.

  • 1
    Welcome to GIS SE! As a new user be sure to take the Tour. Would you be able to edit your question to let us know whether the code you have presented works as far as listing the directories of layer files that you want it to, please? Assuming it does, I think you should next work out the code to take a single layer file and list its file [=feature?] class data source and geometry type. – PolyGeo Sep 7 '15 at 10:35
  • 1
    Welcome to arcpy. You will want to use the arcpy.Describe object to get at the properties you want. The 10.0 version help is located here. Unfortunately you are cutting your teeth on a less user friendly object than most..help.arcgis.com/EN/arcgisdesktop/10.0/help/index.html#//… – Ben S Nadler Sep 8 '15 at 0:09
0

Eventually I used this code:

#importing libraries
import arcpy, os, fnmatch

#defining location of parent folder
BASE_DIRECTORY = 'F:\\'

#scanning through subfolders
for dirpath, dirnames, filenames in os.walk(BASE_DIRECTORY):

 for filename in filenames:

  #defining file type
  if fnmatch.fnmatch(filename, "*.lyr"):

   layerfile_full_path = os.path.join(dirpath, filename)
   layerfile_description = arcpy.Describe(layerfile_full_path)
   layerfile_layer = arcpy.mapping.Layer(layerfile_full_path)
   try:

    layer_map = arcpy.mapping.MapDocument(layerfile_full_path)
    for layer in arcpy.mapping.ListLayers(layer_map):

     layer_data = layer.dataSource.split('\\')
     print(

      #listing file path
      layerfile_full_path,

      #listing server name
      layerfile_layer.serviceProperties['Server'],

      #listing creator name
      layer_data[2],

      #listing Oracle Table (Feature Class)
      layerfile_description.dataElement.name,

      #listing Geometry Type
      layerfile_description.shapeType)

   except:

    #if there are any errors, print 'fail' for these errors
    print(layerfile_full_path, 'fail')

I used the last code because of issues with database connection.

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.