-1

I have a script that runs on a folder to create contour lines. Since I have roughly 2700 DEM which need to be processed, I need a way using the script to run on all folders within the parent folder saving them to an output folder. I am not sure how to script this but it would be greatly appreciated if I could get some guidance.

The following is the script I currently have which works on a single folder.

import arcpy
from arcpy import env
from arcpy.sa import *

env.workspace = "C:/DATA/ScriptTesting/test"

inRaster = "1km17670"
contourInterval = 5
baseContour = 0
outContours = "C:/DATA/ScriptTesting/test/output/contours5.shp"

arcpy.CheckOutExtension("Spatial")

Contour(inRaster,outContours, contourInterval, baseContour)
m00am
  • 5,910
  • 11
  • 53
  • 69
Bree
  • 1

2 Answers2

0

You're probably looking for os.walk(), which can recursively walk through all subdirectories of the given directory. You can either use the current working directory, or calculate your own parent folder and start from there, or whatever - but it'll give you the filenames for everything beneath what it starts with. From there, you can make a subroutine to determine whether or not to perform your script on that file.

Green Cloak Guy
  • 23,793
  • 4
  • 33
  • 53
0

You can get a list of all directories like this:

import arcpy
from arcpy import env
from arcpy.sa import *

import os

# pass in your root directory here
directories = os.listdir(root_dir)

Then you can iterate over this dirs:

for directory in directories:
    # I assume you want the workspace attribute set to the subfolders
    env.workspace = os.path.realpath(directory)

    inRaster = "1km17670"
    contourInterval = 5
    baseContour = 0

    # here you need to adjust the outputfile name if there is a file for every subdir
    outContours = "C:/DATA/ScriptTesting/test/output/contours5.shp"

    arcpy.CheckOutExtension("Spatial")

    Contour(inRaster,outContours, contourInterval, baseContour)

As @a625993 mentioned, os.walk could be useful too if you have recursively nested directories. But as I can read from your question, you have just single subdirectories which directly contain the files and no further directories. That's why listing just the dirs underneath your root directory should be enough.

Igl3
  • 4,900
  • 5
  • 35
  • 69