0

I have 150 shapefiles in folder in "D:\ss3\polygons". I want to save each polygon as .layer file with the polygon name in "D:\ss3\layers"

However, Python displays: "Parsing error SyntaxError: invalid syntax (line 3). I am really confused.

Does anyone have any idea how to fix the SyntaxError and if my code is correct?

import arcpy

#folder locations
in_polygon_folder = "D:\ss3\polygons"
out_layer_folder = "D:\ss3\layers"
arcpy.env.workspace = in_polygon_folder
in_polygon_folder = arcpy.ListFeatureClasses("*", "ALL")

for polygon in in_polygon_folder:
...     polygon_name = polygon[:-4]
...     polygon_name = in_polygon_folder+"\\"polygon_name+"plg.shp"
...     out_layer_folder = out_layer_folder+"\\"+polygon_name+"layer.lyr"
...     arcpy.MakeFeatureLayer_management("buf_layer", out_layer_folder, "256", "NONE", "NOMAINTAIN_EXTENT")

Againg I apologize for my mistake. My code (I include) works well, however, without the for loop I have write 150 lines to save each polygon shp as .lyr file. This my code:

    import arcpy
    from arcpy import env
    env.workspace = r"D:\ss3\polygons"
    buf= r"D:\ss3\polygons\00.shp"
    ou2= r"D:\ss3\layers\002.lyr"

    arcpy.MakeFeatureLayer_management(buf, 'buf_layer')
    arcpy.SaveToLayerFile_management('buf_layer', ou2, "ABSOLUTE")

What I am trying to do is to include my code into a for loop.

4
  • 1
    You've got a number of issues here, including failure to use raw formatting or an escape on the backslash to make the paths valid, but the biggest issue is the confusion indicated by trying to "save" shapefiles as layerfiles -- Layer files only point at data, they don't actually hold the data, so you're only saving the layer as a layerfile, not the shapefile.
    – Vince
    May 19, 2018 at 16:25
  • I apology if my question is unclear. I have this code that works well. However, I have to use my code one by one with 150 polygon shps import arcpy from arcpy import env env.workspace = r"D:\ss3\polygons" buf= r"D:\ss3\polygons\00.shp" ou2= r"D:\ss3\layers\002.lyr" arcpy.MakeFeatureLayer_management(buf, 'buf_layer') arcpy.SaveToLayerFile_management('buf_layer', ou2, "ABSOLUTE") I am trying to include my code into a for loop May 19, 2018 at 18:21
  • 1
    Please do not present revised information and code in a comment. You should use the edit button beneath your question to do that.
    – PolyGeo
    May 19, 2018 at 21:01
  • Be wary of the ABSOLUTE layer files -- If your data is moved (and the layer with it), as when a new letter drve is added, then all the ABSOLUTE layer files will be corrupted, while RELATIVE files will still be valid.
    – Vince
    May 20, 2018 at 2:09

1 Answer 1

0

As stated in the comments by Vince, SaveToLayerFile creates pointers to data only. In case that is what you are looking for, the code below should get you there. The layer file is saved with the base name of the orginal shapefile. I would take the other comments on board as well regarding creating valid paths.

    import arcpy, os
    arcpy.env.workspace = r"D:/ss3/polygons"
    ou2= r"D:/ss3/layers/"

    for fc in arcpy.ListFiles("*.shp"):
        arcpy.MakeFeatureLayer_management(fc, "buf_layer")
        arcpy.SaveToLayerFile_management("buf_layer", ou2 + os.path.splitext(fc)[0] + ".lyr", "ABSOLUTE")
2
  • Hello. I fixed the error with single quotes in ...('buf_layer'....) May 20, 2018 at 18:40
  • good eye.... fixed the quotes in my post.
    – ZrSiO4
    May 20, 2018 at 22:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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