Adding shapefile or feature class as layer in ArcGIS Desktop using Python/ArcPy?

Here's what I found worked:

import arcpy
from arcpy import env

# get the map document
mxd = arcpy.mapping.MapDocument("CURRENT")

# get the data frame
df = arcpy.mapping.ListDataFrames(mxd,"*")[0]

# create a new layer
newlayer = arcpy.mapping.Layer(path_to_shapefile_or_feature_class)

# add the layer to the map at the bottom of the TOC in data frame 0
arcpy.mapping.AddLayer(df, newlayer,"BOTTOM")

The dataframe (variable df) that this code will put the new layer into is the first dataframe in the map document. Also note that this code adds the data as a new layer at the bottom of the TOC. You can also use the other arrangement options, which are "AUTO_ARRANGE" and "TOP".


Make Feature Layer (Data Management) http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//00170000006p000000.htm

I just tried it in the Python window of ArcMap and it adds directly to my map ( I didn't have to get my dataframe and call AddLayer).

arcpy.MakeFeatureLayer_management('r:/temp/a.shp','test') alt text