arcpy examples for multiple files

1574
10
06-13-2011 07:46 PM
SarahBurns
New Contributor
I can not seem to pick up the basics in using arcpy to read in multiple files and then output multiple files.
I can get each process to work on one file (at this stage usually spatial analyst tools) but need help reading in multiple files, executing the process and then outputting the files somewhere. And all of the examples on the ArcGIS10 resource center shows examples with single files.
Is there a (free) tutorial or a place with lots of examples where they use different tools on multiple files.
Thanks
Tags (2)
0 Kudos
10 Replies
MathewCoyle
Frequent Contributor
A fairly broad question, but I thinking looping is what you are looking for.

http://diveintopython.org/file_handling/for_loops.html

If you are having trouble with the basics, my suggestion would be to start at the basics and take the arcpy processing portions out, they can complicate testing if you don't have a firm grip on what python is toiling on about. In place of whatever arcpy function you are calling, do a print statement on the variables that would have been passed into the tool so you see how variables are being passed/manipulated. That's how I learned python, anyway.

The rest of the tutorials on the dive into python page are very good as well.
0 Kudos
LaceyMason
New Contributor
Here is a piece of code I use a lot for shapefiles and raster files.  Let me know if you would like more info.

import os, arcpy

# Input Folder
inputFolder = r"yourfolderpath"

# Output Folder
outputFolder = r"youroutfolderpath"

for inputFilename in os.listdir(inputFolder) :
    if inputFilename.endswith('.shp') :
        inputPath = os.path.join(inputFolder, inputFilename)
        outputPath = os.path.join(outputFolder, inputFilename)
        gp.Clip_analysis(ENTER PARAMETERS)

print "End of processing."



import os, arcpy
from arcpy import env

# Input Folder
inputFolder = r"yourfolderpath"
env.workspace = inputFolder

# Output Folder
outputFolder = r"youroutfolderpath"

rasterList = arcpy.ListRasters("", "GRID")

for raster in rasterList:
    inputPath = os.path.join(inputFolder, raster)
    outputPath = os.path.join(outputFolder, raster)

print "End of processing."


Good Luck!
Lacey
0 Kudos
MikeMacRae
Occasional Contributor III
Which type of files are you trying to open? mxd's? raster's? layers? etc?

If you want to loop through mxd files, this is a common approach

folderPath = r"C:\your mxd location"

# Loop through each folder in the directory to find files containing the extension ".mxd", open each one and process

for filename in glob.glob(os.path.join(folderPath, "*.mxd")):

        fullpath = os.path.join(folderPath, filename)
        mxd = arcpy.mapping.MapDocument(filename)
        
        if os.path.isfile(fullpath):
            basename, filename2 = os.path.split(fullpath)

# At this point, "mxd" is now the variable containing your map document, use that to process whatever it is you're looking to process within each map....
0 Kudos
SarahBurns
New Contributor
Thank you for your feedback.
I am trying to work through python programming:an introduction to computer science by John Zelle
as I would like to learn python 3 (since I am a beginner anyway I might aswell learn the latest version of python)
but I feel that I pick things up quicker if I actually use it and see examples. I agree though, I need the very basics to understand what I am doing.

Yes Lacey Mason I would love some more examples. I am currently working mainly with rasters but will eventually be dealing with polygons.

thanks again
0 Kudos
ChrisMathers
Occasional Contributor III
Python 3 doesnt work with ArcGIS. The latest version of python, 3.x, has a number of syntax changes that would reuire over hauls of code written in 2.x. Esri decided to go with 2.6 for v10 and I heard is going to ship 2.7 with v10.1. If you plan to do a lot of ArcGIS programming I would study up on 2.x
0 Kudos
MathewCoyle
Frequent Contributor
0 Kudos
SarahBurns
New Contributor
thanks, but surely they will move to Python3 soon enough/eventually?
I should just try and stay on top of both! that will be easy enough....
0 Kudos
LoganPugh
Occasional Contributor III
Python 3.x acceptance has been slow in general, so I wouldn't expect it to be forced on ArcGIS users anytime soon, simply due to the lack of support from a large number of other popular Python libraries and frameworks (example).

I would stick to learning Python 2.x -- once you have mastered that, the transition to 3.x, if and when it ever becomes required for ArcGIS, will be much easier.
0 Kudos
SarahBurns
New Contributor
thanks for the response. I think I will take your advice.
0 Kudos