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?

Hornbydd
  • 43,380
  • 5
  • 41
  • 81
user48149
  • 67
  • 2
  • 6

1 Answers1

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
Comrade Che
  • 7,091
  • 27
  • 58