jmdyc000

Need help with Clipping many feature from many folders using a script

Discussion created by jmdyc000 on Oct 15, 2013
Latest reply on Oct 16, 2013 by Wayne_Whitley
I am an amateur at using Python.

I have a recurring task that involves pulling shape files and clipping them to an standard area of interest (AOE).  These shapes are not named consistently and they are stored in sub folders within a folder on a network drive.  I am attempting to write a script to help automate the bulk of this task by generating a list of full path names for every shape file in each sub folder and then clipping each found file using the AOE clip file.  Because the original data is stored on a network drive and others are using the same data I would like the clipped files to be directed to a workspace on my local drive.

I have managed to get the script to function at a basic level with sample data folders, however, I have a big problem.  The clip function is taking the first found shape file in the list and clipping it over and over and over each time renaming it with the correct names from the list of found files.  For example: Sample1.shp is clipped and saved as Sample1_clipped.shp then Sample1.shp is clipped again and saved as Sample2_clipped.shp and Sample3_clipped.shp and 4 and 5 etc..

I cannot figure out where I am going wrong.  Any help would be greatly appreciated.

import os
import arcpy
from arcpy import env

source_dir = r'Z:\Python_scratch\inWorkspace'
extension = '.shp'
workspace_dir = r'Z:\Python_scratch\outWorkSpace'
clip_shp = r'Z:\Python_scratch\ClipFeature.shp'

#Fine path names 
def get_full_paths(dir):
    files = []
    for filename in os.listdir(dir):
        full_path = os.path.join(dir, filename)
        if os.path.isdir(full_path):
            files.extend(get_full_paths(full_path))
        elif filename.endswith(extension):
            files.append(os.path.join(dir, filename))
        else:
            pass

    return files

#Find file names
def get_files(dir):
    files = []
    for filename in os.listdir(dir):
        if os.path.isdir(os.path.join(dir, filename)):
            files.extend(get_files(os.path.join(dir, filename)))
        elif filename.endswith(extension):
            files.append(filename)
        else:
            pass

    return files

my_full_paths = get_full_paths(source_dir)
my_files = get_files(source_dir)
env.workspace = workspace_dir

#Clip all found file from all paths by clip_shp and out put results to output dir
for files in my_files:
    out_filename = "\\" + files.replace(extension, '_clipped' + extension)
    for filename in my_full_paths:
        if arcpy.Exists(out_filename):
            break
        else:
            arcpy.Clip_analysis(filename, clip_shp, workspace_dir + out_filename, '')

Outcomes