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]
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.

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.