2

I have a python script that generates a polygon feature class file and it automatically displays the layer in the table of contents of ArcMap. So whenever I select the layer file in any Arctool as a input, it is showing me duplicate names of the layer. And also, whenever I remove the layer from ArcMap, the lock file of the shapefile doesn't get released unless I close ArcMap down. The code are as follows:

arcpy.CreateFeatureclass_management(outShapefile, shapefileName, "POLYGON", "",
                                "", "", spatial_reference)
arcpy.MakeFeatureLayer_management(outLayer, shapefileName1)
arcpy.ApplySymbologyFromLayer_management(shapefileName1, outLine_Layer)
arcpy.SaveToLayerFile_management(shapefileName1, outLayer2, "ABSOLUTE")

addLayer = arcpy.mapping.Layer(shapefileName1)
arcpy.mapping.AddLayer(df, addLayer, "TOP")

Do you have any idea why its doing this? The most frustrating part is that the locked file persist once the layer is removed from ArcMap.(20171109_2323.shp.SABBILGEDUMP.5028.13292.sr.lock)

1

To remove the lock file, just call arcpy.Delete_management(layer) before the script terminates. If you want to work with a layer file in Arcmap, try to access this layer from arcpy.mapping. It's not need to create a new layer file from the shapefile.

import arcpy
arcpy.env.workspace = "D:/"
arcpy.Delete_management("Myshp.shp")
fc= arcpy.CreateFeatureclass_management("D:/", "Myshp.shp", "POLYGON")
mxd = arcpy.mapping.MapDocument("CURRENT")
listLayer= arcpy.mapping.ListLayers(mxd)
for l in listLayer:
    if l.name == "Myshp":
        arcpy.ApplySymbologyFromLayer_management("Myshp","Mysymbology")
arcpy.SaveToLayerFile_management("Myshp","D:/Loutput1.lyr","Absolute",None)

Without list layers :

import arcpy
    arcpy.env.workspace = "D:/"
    arcpy.Delete_management("Myshp.shp")
    fc= arcpy.CreateFeatureclass_management("D:/", "Myshp.shp", "POLYGON")
    arcpy.ApplySymbologyFromLayer_management("Myshp","Mysymbology")
    arcpy.SaveToLayerFile_management("Myshp","D:/Loutput1.lyr","Absolute",None)
  • The arcpy.Delete_management() will not display the layer on ArcMap's table of contents – Vince Nov 9 '17 at 13:31
  • Do you create layer in-memory? – wetland Nov 9 '17 at 13:53
  • 1
    I edited my answer. I added a sample code. – wetland Nov 9 '17 at 15:59

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.