I've created script in Python for ArcGIS to save shapefile as layer file... the code throws an error:
ERROR 000732: Input Layer: Dataset D:\core i 5 data\symon assignment\Swat\clipped\buffer.shp does not exist or is not supported
Here is my code:
import arcpy
from arcpy import env
env.workspace = "D:/core i 5 data/symon assignment/Swat/clipped"
buf= "D:\core i 5 data\symon assignment\Swat\clipped\buffer.shp"
ou2= "D:\core i 5 data\symon assignment\Swat\clipped\layer1.lyr"
arcpy.SaveToLayerFile_management(buf, ou2, "ABSOLUTE")
-
Not an expert in arcpy but I am pretty sure the paths cannot contain spaces and backspaces are escape signs so they need to be doubled to be taken literally. e.g. "D:\\file\\path\\layer1.lyr".Kersten– Kersten2016年04月15日 10:20:55 +00:00Commented Apr 15, 2016 at 10:20
1 Answer 1
Save to Layer File saves a Layer (in memory, layer from disk, or layer from ArcMap) to a layer (.lyr) file. It won't work directly on a feature class or shapefile. You need to first use Make Feature Layer. See Save to Layer File and Make Feature Layer on ArcGIS Desktop Help.
import arcpy
from arcpy import env
env.workspace = r"D:/core i 5 data/symon assignment/Swat/clipped"
buf= r"D:\core i 5 data\symon assignment\Swat\clipped\buffer.shp"
ou2= r"D:\core i 5 data\symon assignment\Swat\clipped\layer1.lyr"
arcpy.MakeFeatureLayer_management(buf, 'buf_layer')
arcpy.SaveToLayerFile_management('buf_layer', ou2, "ABSOLUTE")
Also, as mentioned in @Kersten's comment above, you need to be careful with back-slash in path names. Either reverse them using a forward-slash /
, double back-slash \\
, or prefix the string with an r
.