1

I have a script I am using to save out layers automatically and would like some help with understanding where my files are being saved out to.

In my current code, the file that I am processing is being picked up in the arcpy.env.workspace.

But the layers that I am saving out are being saved in the same place. How do change where the files are being saved ?

import arcpy
from arcpy import env

#Variables which need to pick for this are the attribute that you are going to   filter the layers on

# This is where the file is being picked up from 
arcpy.env.workspace = "U:\\Script\\layers_Files\\"
# The file to be processed 
Source_File = "feilds_template.shp"

Company_Bulk_Company_List = [row[0] for row in arcpy.da.SearchCursor   (Source_File,"HC_TYPE")]
UniqueCompany = set(Company_Bulk_Company_List)
print UniqueCompany

 # Getting the list out 
 for company_query in UniqueCompany:

 #env.workspace = "S:\\AppsData\\GIS\\PROJECT\\COMPANIES\\SHAPEFILES\\"

    out_layer0 = str(company_query)+"lyr"
    in_layer = out_layer0
    out_layer = in_layer +"lyr"

 #MakeFeatureLayer variables
    in_features = "IHS_FEILDS_AREAS.shp"

    where_clause = '"HC_TYPE" = ' + "'" + company_query + "'"
    print where_clause
    workspace = " U:\\Script\\layers_Files\\"

    try:
    # Execute MakeFeatureLayer
        arcpy.MakeFeatureLayer_management(in_features, out_layer0, where_clause, workspace)

    # Execute SaveToLayerFile
         arcpy.SaveToLayerFile_management(in_layer, out_layer, "ABSOLUTE")
    except:
        print arcpy.GetMessages()
3
  • 1
    My noob guess is workspace = " U:\\Script\\layers_Files\\", before your try statement. I don't use Arc but your first algorithm "Execute MakeFeatureLayer" looks like it's outputting the results in the workspace location. This is just a guess =)
    – Joseph
    Apr 24, 2015 at 9:59
  • I have tried changing the above the try statement , but little saves out the files in the arcpy.env.workspace , where the file is picked up Apr 24, 2015 at 10:06
  • Hopefully others can help advise you on this issue :)
    – Joseph
    Apr 24, 2015 at 10:11

1 Answer 1

1

You need to change the value of out_layer to include the output path. Add import os to the start of your script and then set the out_layer variable as follows:

out_layer = os.path.join("your/output/path/here", in_layer + "lyr")
1
  • This does save out the first layer into the folder. But it is only saving 1 layer . Previously it would save all layers automatically Apr 24, 2015 at 11:36

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.