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:
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
"RasterDataset"
as datatype in your walk function, use"FeatureClass"