I am trying to add a Feature Class from a Geodatabase Dataset to an MXD and save a copy like this but I am getting following errors on each methos
import arcpy
import os
arcpy.env.workspace = r"C:\arcgis\ArcTutor\AAA\MXDs"
MXD = arcpy.mapping.MapDocument(r"C:\arcgis\ArcTutor\AAA\MXDs\map.mxd")
DF = arcpy.mapping.ListDataFrames(MXD)[0]
lyr = arcpy.mapping.Layer("C:\arcgis\ArcTutor\AAA\GDBs\Delaware.gdb\Kent\Kent")
arcpy.mapping.AddLayer(DF, layer, "AUTO_ARRANGE")
MXD.saveACopy('newmxd.mxd')
del MXD
print(" Done")
and
import arcpy import os arcpy.env.workspace = r"C:\arcgis\ArcTutor\AAA\MXDs" MXD = arcpy.mapping.MapDocument(r"C:\arcgis\ArcTutor\AAA\MXDs\map.mxd") DF = arcpy.mapping.ListDataFrames(MXD)[0] lyr = "C:\arcgis\ArcTutor\AAA\GDBs\Delaware.gdb\Kent\Kent" arcpy.MakeFeatureLayer_management(lyr, 'shplyr') arcpy.mapping.AddLayer(DF, lyr, "AUTO_ARRANGE") print(" Step 4") MXD.saveACopy('newmxd.mxd') del MXD print(" Done")
Solved! Go to Solution.
Try:
import arcpy
import os
arcpy.env.workspace = r"C:\\arcgis\\ArcTutor\\AAA\\MXDs"
MXD = arcpy.mapping.MapDocument(r"C:\\arcgis\\ArcTutor\\AAA\\MXDs\\map.mxd")
DF = arcpy.mapping.ListDataFrames(MXD)[0]
fc = "C:\\arcgis\\ArcTutor\\AAA\\GDBs\\Delaware.gdb\\Kent\\Kent"
lyr = arcpy.MakeFeatureLayer_management(fc, 'shplyr')[0]
arcpy.mapping.AddLayer(DF, lyr, "AUTO_ARRANGE")
print(" Step 4")
MXD.saveACopy('newmxd.mxd')
del MXD
print(" Done")
you were doing so well on the raw encoding, then you stopped
"C:\arcgis\ArcTutor\AAA\GDBs\Delaware.gdb\Kent\Kent"
'C:\x07rcgis\\ArcTutor\\AAA\\GDBs\\Delaware.gdb\\Kent\\Kent'
notice what \a produces \x07rcgis
raw encode, double backslashes or single forward slashes
Hi Thank and Thanks for reply I changed the code to
import arcpy
import os
arcpy.env.workspace = r"C:\\arcgis\\ArcTutor\\AAA\\MXDs"
MXD = arcpy.mapping.MapDocument(r"C:\\arcgis\\ArcTutor\\AAA\\MXDs\\map.mxd")
DF = arcpy.mapping.ListDataFrames(MXD)[0]
lyr = "C:\\arcgis\\ArcTutor\\AAA\\GDBs\\Delaware.gdb\\Kent\\Kent"
arcpy.MakeFeatureLayer_management(lyr, 'shplyr')
arcpy.mapping.AddLayer(DF, lyr, "AUTO_ARRANGE")
print(" Step 4")
MXD.saveACopy('newmxd.mxd')
del MXD
print(" Done")
and now I am getting AssertionError: on assert isinstance(add_layer, Layer)
File "C:\Program Files (x86)\ArcGIS\Desktop10.6\ArcPy\arcpy\mapping.py", line 49, in AddLayer
assert isinstance(add_layer, Layer)
AssertionError
line 6 isn't a layer as in *.lyr
line 7 you make a feature layer.. form lyr, and you call it 'shplyr'... did you try that in line 8? since line 6 won't work.
also be prepared that AddLayer with become addLayer in ArcGIS Pro... subtle differences
Try:
import arcpy
import os
arcpy.env.workspace = r"C:\\arcgis\\ArcTutor\\AAA\\MXDs"
MXD = arcpy.mapping.MapDocument(r"C:\\arcgis\\ArcTutor\\AAA\\MXDs\\map.mxd")
DF = arcpy.mapping.ListDataFrames(MXD)[0]
fc = "C:\\arcgis\\ArcTutor\\AAA\\GDBs\\Delaware.gdb\\Kent\\Kent"
lyr = arcpy.MakeFeatureLayer_management(fc, 'shplyr')[0]
arcpy.mapping.AddLayer(DF, lyr, "AUTO_ARRANGE")
print(" Step 4")
MXD.saveACopy('newmxd.mxd')
del MXD
print(" Done")