I'm trying to build a Python Script that will receive one feature layer that will be clipped by several adjacent grid squares (each in a different shapefile) and output several shapefiles.

Now, I've built in the past a loop script that receives a list of files and does a single action on each file, like this:

for file in os.listdir(ws):
    if file.endswith(".shp") or file.endswith(".dxf") or file.startswith("str"):
    print file

But I'm not succeeding in making a loop that will receive the input shapefile and clip features (shapefiles) and EXPORT to different shapefiles (I mean how do I change the output shapefile's name for every grid square ?). Can someone show how it is done?

I'm using python 2.6 and Arcgis 9.3.1


I guess this depends on how often you want to run the script, whether or not you want to hard code in the clip grids and the output shapefiles.

Very simply, I'd have a dictionary, of clip_grid : output_shapefile, loop through that and spit out the output.

import arcgisscripting
#Create the geoprocessor
gp = arcgisscripting.create(9.3)

input_shape = "yourshapefile.shp" #<-- this single file will be clipped repeatedly

#grids and corresponding outputs go here
clip_dictionary = {"clipgrid1.shp":"output1.shp", "clipgrid2.shp":"output2.shp"}

for iter in clip_dictionary:
    #Clip_analysis (in_features, clip_features, out_feature_class, cluster_tolerance)
    gp.Clip_analysis(input_shape, iter, clip_dictionary[iter])

Hope that helps!

sys.argv is a possiblity if you wish to read in the arguments from the command line (or if you wish to add the script to the toolbox). You could also use getopt or optparse as well.

Assume that we have a script called clip_export.py, which we wish to pass a list of inputs. We could run the script like:

clip_export.py input_shape clipshape_1;clipshape_2;clipshape_3 output_folder

Note that this is similar to the input that is sent to a python script by Toolbox.

At any rate, you can read this in your script like so:

import sys

input_shape = sys.argv[1] #<-- Note that the 0th argument in sys.argv is the script name

clip_shape_list = sys.argv[2].split(';') #<-- splits the second argument on the semicolon into a python list

output_folder = sys.argv[3]

The from here run the rest of your script. As @Simon Norris shows below.

Again, hope this helps!

  • 2
    for iter in clip_dictionary: ... can be written as for grid, output in clip_dictionary.iteritems() where grid and output are the respective key and value. – relet Aug 30 '10 at 9:00
  • As My ArcGIS has problems with the first row ( I have no idea why), I didn't test it, but I must say I thought there would be a easier way ( I mean, I like Excel and concatenate, but it's a lot of work to prepare dictionaries). Thanks. – jonatr Aug 30 '10 at 11:50
  • You say you're using Python 2.6 - use the Python 2.5 that's included with the ArcGIS install. I'm pretty sure that arcgisscripting for 9.3 doesn't work with Python 2.6. – om_henners Aug 30 '10 at 15:32
  • If you wanted to make getting the file names easier you could read them from the command line with sys.argv – om_henners Aug 30 '10 at 15:37
  • om, but how do I read from sys.argv and feed it to a dictionary? – jonatr Aug 31 '10 at 13:22

You can create the output filename from the input filename using string indexing and concatenation, or the replace method.


clip_dictionary = {}
for file in os.listdir(r"c:/myclippingshapes"): 
    if file.endswith(".shp"):
        clip_dictionary[file] = file[:file.find(".shp")]+"_out.shp"
        clip_dictionary[file] = file.replace(".shp", "_out.shp")

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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