This should work; it worked for me. This will give you a group layer named by the folder and a layer with the name of the shapefile. I built off what artwork21 posted.
import os
import arcpy
WS = r'c:\test_define'
arcpy.env.workspace = "in_memory"
mxd = arcpy.mapping.MapDocument(r"c:\test_define\test_python_add2.mxd")
df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0]
for (path, dirs, files) in os.walk(WS):
for name in files:
if name.endswith(".shp"):
groupLayer = arcpy.mapping.Layer(r"c:\test_define\empty_group_layer.lyr")
arcpy.mapping.AddLayer(df, groupLayer, "BOTTOM")
layers = arcpy.mapping.ListLayers(mxd)
for layer in layers:
if layer.isGroupLayer and layer.name == "New Group Layer":
layer.name = os.path.basename(path)
newLayer = arcpy.mapping.Layer(os.path.join(path, name))
arcpy.mapping.AddLayerToGroup(df, layer, newLayer, "BOTTOM")
mxd.save()
You have to make the shapefiles into layers and use AddLayerToGroup.
If you want a single group layer for each folder you can recycle this code and modify the os.walk to check for folders with .shp's and go from there. Although, I would recommend trying to contain all these shapefiles into a geodatabase with a proper naming structure and metadata to fit your needs.
To elaborate on the nonrepeating,
import arcpy
import os
WS = r'c:\test_define'
arcpy.env.workspace = "WS"
mxd = arcpy.mapping.MapDocument(r"c:\test_define\test_python_add2.mxd")
df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0]
groupLayer = arcpy.mapping.Layer(r"c:\test_define\empty_group_layer.lyr")
for (path, dirs, files) in os.walk(WS):
for name in files:
if name.endswith(".shp"):
layers = arcpy.mapping.ListLayers(mxd)
name_list = []
for layer in layers:
if layer.isGroupLayer:
name_list.append(layer.name)
if os.path.basename(path) not in name_list:
arcpy.mapping.AddLayer(df, groupLayer, "BOTTOM")
layers = arcpy.mapping.ListLayers(mxd)
for layer in layers:
if layer.isGroupLayer and layer.name == "New Group Layer":
layer.name = os.path.basename(path)
newLayer = arcpy.mapping.Layer(os.path.join(path, name))
arcpy.mapping.AddLayerToGroup(df, layer, newLayer, "BOTTOM")
if os.path.basename(path) in name_list:
layers = arcpy.mapping.ListLayers(mxd)
for layer in layers:
if layer.isGroupLayer and layer.name == os.path.basename(path):
newLayer = arcpy.mapping.Layer(os.path.join(path, name))
arcpy.mapping.AddLayerToGroup(df, layer, newLayer, "BOTTOM")
mxd.save()
This will check to see if a group layer already exists and will add to it, if so. Else, it will create a new group layer and add the layer.
First off, I don't think you want to be saving these as layer files (.lyr). A layer file is only a pointer to data. You need to save the data to a feature class or shapefile. The output from the Make XY event layer tool is a "in memory layer" and it is gone once the session is over. That needs to be converted to feature class to save it to your computer.
Try something like this:
arcpy.CopyFeatures_management(out_Layer, "C:/project/year_hour.shp")
The error your getting is probably related to either running out of memory, or because your trying to create an "in-memory" layer that already exists. try deleting the in memory layer after your are done with it:
arcpy.Delete_management(out_Layer)
Best Answer
Suggestions:
os.path.join
to join folder paths to filenames..format()
method or the older modulo (%
) method) instead of the concatentation operator (+
)Here's an example of combining the first two suggestions (using the older modulo string formatting which is what I'm more familiar with):