hm870

Looping through sub folders in a python script

Discussion created by hm870 on Mar 4, 2011
Latest reply on Mar 11, 2011 by hm870
Hi everyone,

I'm using a script to define the projection of unprojected shapefiles. The shapefiles are grouped in different directories and the script works well except with two folders. The shapefiles in those folders are in sub-folders and the script skips them.

Now I would like to find a way to make the script loop through those sub folders.

I should mention that I'm not that experienced with scripting and most of what I have was taken from other scripts and adapted to what I wanted to accomplish. If you have any comments that could help me make this script better, let me know.

Thank you in advance for your help.



Here's the script:


# Import system modules
import sys, string, os, arcgisscripting

# Create the Geoprocessor object
gp = arcgisscripting.create()

# Load required toolboxes...
gp.AddToolbox("C:/Program Files/ArcGIS/ArcToolbox/Toolboxes/Data Management Tools.tbx")

# Local variables...
dirname = "Z:\\01_Data\\04_Vector\\04_World\\DAFIF\\AVDAFIF"
datum = "GEOGCS['GCS_WGS_1984',DATUM['D_WGS_1984',SPHEROID['WGS_1984',6378137.0,298.257223563]],PRIMEM['Greenwich',0.0],UNIT['Degree',0.0174532925199433]]"
folder = os.listdir(dirname)

X = 0

# Process: Define Projection...
for i in folder:
    print "working in the", folder[X], "folder"
    gp.workspace = "Z:\\01_Data\\04_Vector\\04_World\\DAFIF\\AVDAFIF\\" + str(folder[X])
    shape = gp.ListFeatureClasses()
    shapefileName = shape.next()

    while shapefileName:
        workShape = gp.workspace + "\\" + shapefileName
        print "defining projection for " + shapefileName + "..."
        gp.DefineProjection_management(workShape, datum)
        shapefileName = shape.next()
    else:
        print "Next Folder"

    X = X + 1

print "Done"

Outcomes