0

I am trying to convert several shapefiles (in folder G:\desktop\Project\lyr) into layer files using this python code:

import arcpy,os
from arcpy import env

env.workspace = r"G:\desktop\Project\lyr"
env.overwriteOutput = True
for files in arcpy.ListFeatureClasses():
    filesPath = arcpy.Describe(files).catalogPath
    arcpy.MakeFeatureLayer_management( filesPath,"lyrName")
    arcpy.SaveToLayerFile_management("lyrName",os.path.splitext(filesPath)[0] + ".lyr")

the code work fine, and all the shapefiles converted into layer files with the same name of the shapefiles:

enter image description here

But when I load those layers files into the MXD file, all the layers have the same name, whereas I expected each layer will have a specific name:

enter image description here

I really don't understand what am I doing wrong.

3

Your MakeFeatureLayer line names all your layers as "lyrName". The second parameter of that tool needs to be given values that are unique for each layer.

If you don't uniquely name your layers, you have no idea what ArcGIS will do with requests to access a layer by name.

import arcpy,os 
from arcpy import env 

env.workspace = r"G:\desktop\Project\lyr"
env.overwriteOutput = True
for files in arcpy.ListFeatureClasses(): 
    filesPath = arcpy.Describe(files).catalogPath
    lyrName = os.path.splitext(filesPath)[0]
    arcpy.MakeFeatureLayer_management(filesPath,lyrName)
    arcpy.SaveToLayerFile_management(lyrName,lyrName + ".lyr")

There is no correspondence between the source file name, layer name, and layerfile name except as you declare them.

  • Thanks Vince, but i get this result: layer1, layer2, layer3- whereas i need the original names in the table of content: frame, line, point3hp- how can i get this result? – newGIS May 27 '18 at 13:43
  • You've twice missed the point. The layer description is the name you give it. – Vince May 27 '18 at 14:12
  • i understand that. i just asking if it possible to change the layer name as i need ? – newGIS May 27 '18 at 14:16
  • That would be a different question. – Vince May 27 '18 at 14:28

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.