1

In this code i try to add layer files- only if the first character in the name file, is a numbers to a MXD file, but an error happened:

import os,sys,fnmatch,shutil,arcpy

rootPath = r"G:\desktop\Project\lyr\taba\7650"
mxd = arcpy.mapping.MapDocument(r"G:\desktop\Project\Project.mxd")
df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0]
for root, dirs, filenames in os.walk(rootPath):
    for d in dirs:
        DIR = os.path.join(root,d)
        lyr_files = fnmatch.filter(os.listdir(DIR), '*.lyr')
        for i in lyr_files:
            if i[0].isdigit():
                addLayer = arcpy.mapping.Layer(i)
                arcpy.mapping.AddLayer(df, addLayer, "BOTTOM") 
                mxd.save()

UPTADE: I fix the code according to Hornbydd and still got the same error:

     for i in lyr_files:
            if i[0].isdigit():
                addLayer = arcpy.mapping.AddLayer(df, arcpy.mapping.Layer(os.path.join(root, i)))
                arcpy.mapping.AddLayer(df, addLayer, "BOTTOM") 
                mxd.save()    

this is the error:

Traceback (most recent call last):
  File "G:\desktop\abc.py", line 68, in <module>
addLayer = arcpy.mapping.Layer(i)
  File "C:\Program Files (x86)\ArcGIS\Desktop10.3\ArcPy\arcpy\arcobjects\mixins.py", line 389, in __init__
super(LayerMixin, self).__init__(lyrfile)
  File "C:\Program Files (x86)\ArcGIS\Desktop10.3\ArcPy\arcpy\arcobjects\_base.py", line 47, in __init__
for arg in args))
ValueError: Object: CreateObject Layer invalid data source
>>> 
  • 1
    You were nearly there! See my amended answer. Note you were (1) referencing the wrong folder variable, (2) Creating the layer adding it then adding it again and (3) saving on every iteration of the loop which is inefficient. – Hornbydd Jun 4 '18 at 20:22
2

This line is creating a list of Layer names:

lyr_files = fnmatch.filter(os.listdir(DIR), '*.lyr')

If you read the Help file for Layer (arcpy.mapping) under the syntax section is says:

A string that includes the full path and file name of an existing layer (.lyr) file.

Your code is looping over a list of file names only and not full paths, You need to concatenate the file name (i in your case) with it's path to create a full path to your layer file.

Your final code would be:

import os,sys,fnmatch,shutil,arcpy

rootPath = r"G:\desktop\Project\lyr\taba\7650"
mxd = arcpy.mapping.MapDocument(r"G:\desktop\Project\Project.mxd")
df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0]
for root, dirs, filenames in os.walk(rootPath):
    for d in dirs:
        DIR = os.path.join(root,d)
        lyr_files = fnmatch.filter(os.listdir(DIR), '*.lyr')
        for i in lyr_files:
            if i[0].isdigit():
                addLayer = arcpy.mapping.Layer(os.path.join(DIR, i))
                arcpy.mapping.AddLayer(df, addLayer, "BOTTOM")
mxd.save()
  • can you please show me how to do it? – newGIS Jun 4 '18 at 10:50
  • To concatenate a string simply add them together, A+B = C. – Hornbydd Jun 4 '18 at 13:31
0

Based on a previous answer by @Radar, try creating a feature layer first.

Your code, modified:

import os,sys,fnmatch,shutil,arcpy

rootPath = r"G:\desktop\Project\lyr\taba\7650"
mxd = arcpy.mapping.MapDocument(r"G:\desktop\Project\Project.mxd")
df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0]
for root, dirs, filenames in os.walk(rootPath):
    for d in dirs:
        DIR = os.path.join(root,d)
        lyr_files = fnmatch.filter(os.listdir(DIR), '*.lyr')
        for i in lyr_files:
            if i[0].isdigit():
                arcpy.MakeFeatureLayer_management(os.path.join(root, i), "nameoffeatureclass")
                addLayer = arcpy.mapping.Layer("nameoffeatureclass")
                arcpy.mapping.AddLayer(df, addLayer, "BOTTOM") 
                mxd.save()

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.