1

I am working on an application and I am a beginner in Python :(

I want to add a shapefile, the execution is successful and everything but no result appears on ArcMap, here is the code I use:

import arcpy

try:

    mxd = arcpy.mapping.MapDocument("CURRENT")

    df = arcpy.mapping.ListDataFrames(mxd)[0]


    newlayer1 = arcpy.mapping.Layer(E:\DATA SF\SFcircuit\shape_zizo\batiment.shp")
    print newlayer1
    arcpy.mapping.AddLayer(df, newlayer1, "TOP")  

    arcpy.RefreshActiveView()
    arcpy.RefreshTOC()

    # save the map to persist the added layer
    mxd.save()



except Exception as ex:
    print ex.args[0]
PolyGeo
  • 65,136
  • 29
  • 109
  • 338
user24541
  • 69
  • 2
  • 4

2 Answers2

6

I think this line:

newlayer1 = arcpy.mapping.Layer(E:\DATA SF\SFcircuit\shape_zizo\batiment.shp")

needs to be:

newlayer1 = arcpy.mapping.Layer(r"E:\DATA SF\SFcircuit\shape_zizo\batiment.shp")

As commented by @Llaves:

r indicates a raw string literal. Without the r, the backslash \ indicates an escape sequence, such as a newline.

PolyGeo
  • 65,136
  • 29
  • 109
  • 338
4

Just run MakeFeatureLayer in the Python window:

mxd = arcpy.mapping.MapDocument("current")
arcpy.management.MakeFeatureLayer_management(r"E:\DATA SF\SFcircuit\shape_zizo\batiment.shp", "batiment")
mxd.save()
Jason Scheirer
  • 18,002
  • 2
  • 53
  • 72