1

I am really stuck with this one and cannot seems to get the code to execute. Every time I do so, I get the error: Input Features: Dataset [u'Test1'.....] does not exist or is not supported. My guess is that it is the latter as I have tested the code with a arcpy.Exists command and the code executes but does not populate the data frame appropriately.

Code:

  import arcpy, os

  arcpy.env.workspace = r'c:\pathway to geodatabase'
  fcs = arcpy.ListFeatureClasses()

  mxd = arcpy.mapping.MapDocument(r'C:\pathway to mxd')
  DF = arcpy.mapping.ListDataFrames(mxd,'*')[0]

  for fc in fcs:      
     inputlayer = fcs
     outlayer = os.path.join(*(fcs) + '.lyr')
     arcpy.MakeFeatureLayer_management(inputlayer,outlayer)

     layer = arcpy.mapping.Layer(fc)
     arcpy.mapping.AddLayer(DF,layer,"AUTO_ARRANGE")

When I print fcs, this produces a string with the 'u' with unicode preceding the actual value. I did try to convert it to ASCII and got the follow error: AttributeError: 'MapDocument' object has no attribute 'encode'

Where am I going wrong?

2

First of all you have some errors in your code:

inputlayer = fcs have to be changed inputlayer = fc

At my mind the syntax is wrong here: os.path.join(*(fcs) + '.lyr')

Here is a working example of your code, it takes mxd file and add every feature class from GDB to mxd file and saves it:

import arcpy, os

arcpy.env.workspace = r"C:\New File Geodatabase.gdb"
fcs = arcpy.ListFeatureClasses()

mxd = arcpy.mapping.MapDocument(r"C:\1.mxd")
DF = arcpy.mapping.ListDataFrames(mxd,'*')[0]

for fc in fcs:
    inputlayer = fc
    outlayer = os.path.join(fc + '.lyr')
    print outlayer
    arcpy.MakeFeatureLayer_management(inputlayer, outlayer)

    layer = arcpy.mapping.Layer(fc)
    arcpy.mapping.AddLayer(DF, layer, "AUTO_ARRANGE")

mxd.save()
del mxd

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.