# How to add shapefile as layer to map using Python and ArcPy? [duplicate]

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.RefreshActiveView()
arcpy.RefreshTOC()

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

except Exception as ex:
print ex.args[0]


## marked as duplicate by PolyGeo♦Oct 11 '15 at 8:30

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")


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

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()