2

I am writing a Python script, which can literate through all main folder and Subfolder. My main folder names are 2001, 2002.....and under main folder differents subfolder is there, Subfolder name : JANUARY, FEBRAURY so on, So my existing script I can iterate all subfolder under one main folder, but I can't iterate through all the main and subfolder at the same time, Below is my script:

Folder and subfolder structure like this: enter image description here

import arcpy, os, calendar
from arcpy import env
from arcpy.sa import *
arcpy.CheckOutExtension("Spatial")
topWorkspace = r'D:\Workspace\2006'
env.workspace = topWorkspace
months = {calendar.month_name[i].upper(): str(i).zfill(3) for i in range(1, 13)} 

 # Step through list of all folders
for folderPath in arcpy.ListWorkspaces():
    baseName = os.path.basename(folderPath).upper()
    if baseName in months: # Test that subfolder is a month name
        monthNumber = months[baseName] # Get month-number for use in output filename
    arcpy.env.workspace = folderPath

    #Loop every month folder ta take shape file from each month 
    shapeFile = (arcpy.ListFeatureClasses('rain_*.shp')[0])

I have updated the code using arcpy.da.walk but its producing error

    import arcpy, os, calendar
    from arcpy import env
    from arcpy.sa import *
    arcpy.CheckOutExtension("Spatial")
    topWorkspace = r'D:\Workspace\2006'
    env.workspace = topWorkspace
    months = {calendar.month_name[i].upper(): str(i).zfill(3) for i in range(1, 13)} 
    # Get list of all folders 
   walk = arcpy.da.Walk(topWorkspace, topdown=True,datatype="FeatureClass")
     # Step through list of all folders
    for folderPath, folderNames, fileNames in walk:
        baseName = os.path.basename(folderPath).upper()
        if baseName in months: # Test that subfolder is a month name
            monthNumber = months[baseName] # Get month-number for use in output filename
        arcpy.env.workspace = folderPath

        #Loop every month folder ta take shape file from each month 
        shapeFile = (arcpy.ListFeatureClasses('rain_*.shp')[0])

Error message:

File "<module2>", line 27, in <module>
IndexError: list index out of range

How to address here two loop condition for main and sub folder

closed as too broad by Vince, whyzar, mgri, nmtoken, Mapperz Mar 15 '17 at 14:11

Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 3
    use the arcpy.da.Walk() function – GISGe Mar 15 '17 at 10:34
  • I am getting confused Which one first i have to consider Main folder or Subfolder ? – SWAT Mar 15 '17 at 11:00
  • A nested looping structure always requires the parents to be in the outer loop. You haven't attempted an inner loop. – Vince Mar 15 '17 at 11:20
  • 1
    You're looking for shapefiles here, so instead of using "RasterDataset" as datatype in your walk function, use "FeatureClass" – GISGe Mar 15 '17 at 11:41
  • 1
    You now have two questions in one Question. Please choose one solution or the other. – Vince Mar 15 '17 at 12:05
2

This is how your script should look like. Make sure you understand the 3 items in the tuple returned by the Walk function:

  • dirpath is the path to the workspace as a string.
  • dirnames is a list of names of subdirectories and other workspaces in dirpath.
  • filenames is a list of names of nonworkspace contents (i.e., files) in dirpath.

    import arcpy, os, calendar
    from arcpy import env
    from arcpy.sa import *
    
    topWorkspace = r"D:\Workspace"
    env.workspace = topWorkspace
    months = {calendar.month_name[i].upper(): str(i).zfill(3) for i in range(1, 13)}
    # Get list of all folders
    walk = arcpy.da.Walk(topWorkspace, topdown=True, datatype="FeatureClass")
     # Step through list of all folders
    for folderPath, folderNames, fileNames in walk:
        for folderName in folderNames:
            if folderName in months:
                env.workspace = os.path.join(folderPath, folderName)
                if len(arcpy.ListFeatureClasses('rain_*.shp')) > 0:
                    shapeFile = arcpy.ListFeatureClasses('rain_*.shp')[0]
                    ## then do something with each shapefile, e.g. here copy it to another location
                    arcpy.CopyFeatures_management(shapeFile, os.path.join(r"C:\Temp", shapeFile[:-4] + "_copy.shp"))
    

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