0

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.

2
  • 3
    Do you have a typo in your post or in the script? (the nested for isn't aligned correctly)
    – Vince
    Commented Sep 18, 2015 at 1:11
  • No. It's nested correctly in PythonWin. Just missed a space when formatting it as code for the post.
    – geoJshaun
    Commented Sep 18, 2015 at 1:50

1 Answer 1

1

I think the error that you are seeing is due to a missing space before the #:

Instead of:

selection = arcpy.SelectLayerByAttribute_management(input,year)#creates variable based on 

try:

selection = arcpy.SelectLayerByAttribute_management(input,year) #creates variable based on 

However, I think this will just move the error because I don't think the result object returned by arcpy.SelectLayerByAttribute_management() will be iterable in the way that you are trying.

I would make the selection using:

arcpy.SelectLayerByAttribute_management(input,year)

and then use a Search Cursor on the layer instead.

2
  • I left out the outer parentheses in os.mkdir(). Corrected it and now I am getting this error:TypeError: 'NoneType' object has no attribute 'getitem'
    – geoJshaun
    Commented Sep 18, 2015 at 15:01
  • Okay. So I should make a layer from the selection and then use search cursor. I misread your post.
    – geoJshaun
    Commented Sep 18, 2015 at 21:06

Your Answer

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

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