I'm writing a script that will create a list of values from the field "Year_" using search cursor, and then a set() of unique values from the list. I then want to iterate through the set making a main folder with the current iteration value for "Year_" followed by the creation of a selection variable also based on the value of the current iteration. From this selection I want a second list made based on the values in the "Customer" field and then a set of unique values from the list. Finally, the script should make sub folders of unique customer names based on the unique values in the set:
# Import modules
import arcpy, os , sys, string
# Create environmental variables
arcpy.env.overwriteOutput = True
arcpy.env.workspace = r"C:\data.gdb"
# Set variables
input = r"C:\data.gdb\layer"
yearField = "Year_"
custField = "Customer_Name"
rootpath1 = r"S:\Shared\Orders"
# Use Search Cursor to make list of years and then make set of years with unique values
yearList = [row[0] for row in arcpy.da.SearchCursor(input,(yearField))]
yearSet = set(yearList)
#Make main year directories
for year in yearSet:
rootpath2 = os.mkdir(os.path.join(rootpath1,year) #establishes root path for subfolders while making main directories
selection = arcpy.SelectLayerByAttribute_management(input,year)#creates variable based on current selected year in input layer
for customer in selection:
customerList = [row[0] for row in arcpy.da.SearchCursor(input,(custField))] #Use Search Cursor to make list of customers and then make set of customers with unique values
customerSet = set(customerList)
for name in customerSet:
os.mkdir(os.path.join(rootpath2,name) #makes unique customer sub folders
So if the values for years are 2010, 2010, 2011, 2012, 2013, 2013 in the field "Year_" a list will be made and the resulting set would be [2010,2011,2012,2013]. The first iteration would find 2010 and make the pathway r"S:\Shared\Orders\2010".
While in this iteration, 2010 would be used to select by attributes. If customers Joe, Joe, Ann, Bob, Mary, Mary, Mary were found a list would be made and then the set [Joe, Ann, Bob, Mary]. Finally a for loop will iterate through the set and make the pathways:
r"S:\Shared\Orders\2010\Joe" r"S:\Shared\Orders\2010\Ann" r"S:\Shared\Orders\2010\Bob" r"S:\Shared\Orders\2010\Mary" However, I get a an error "Failed to run script - syntax error - invalid syntax at the line where I make the selection variable.
for
isn't aligned correctly)