1

I am trying to add shapefile using ArcPy, but each time my script is ending in showing runtime error? I am using ArcGIS 10.

Here is my script

import arcpy
from arcpy import env

Master="G:\path\To\ShapeFile.shp"

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

df = arcpy.mapping.ListDataFrames(mxd,"*")[0]
layer = arcpy.mapping.Layer(Master)
arcpy.mapping.AddLayer(df, layer,"BOTTOM")

arcpy.RefreshActiveView()
arcpy.RefreshTOC()

This script works fine when I add .lyr files, but why it isn't working for .shp

  • What is your path to shapefile? Could it be something special in the path string? Try the same script with C:\GIS\Simple.shp to make sure it works with the simple path. – Alex Tereshenkov Mar 3 '16 at 9:37
  • 1
    I assume you run the script in the ArcMap session? When I do with my shapefile, the code you've published works. – Alex Tereshenkov Mar 3 '16 at 9:38
  • I am running it in ArcMap's python window, therefore, yes in the ArcMap session. Mine is ArcGIS 10. What version you're using? @AlexTereshenkov – Mario Mar 3 '16 at 10:06
1

Try:

Master = r"G:\path\To\ShapeFile.lyr"

arcpy.mapping.Layer only works with .lyr files acccording to Esri documentation but it seems to accept .shp files as well.

See here for info about raw strings

  • I already tried this, it's also giving the same error. – Mario Mar 3 '16 at 9:22
  • The function only accepts .lyr files. – Nxaunxau Mar 3 '16 at 9:38
  • What to do for .shp files? – Mario Mar 3 '16 at 9:47
  • Sorry, that script should work with shapefiles....can you post the full error msg? – Nxaunxau Mar 3 '16 at 9:52
  • Question title itself shows the full error message, what I see on the ArcGIS python window. – Mario Mar 3 '16 at 9:54
-1

Problem with your path, so check that.

Change to Master=r"G:\path\To\ShapeFile.shp"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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