Hi,
I am new to python and I'm trying to use the python interface in ArcGIS to loop a function (buffer) over all files in a directory that I specified. I come up with the script below. However, when I run it, no errors are returned, but no files are created, nothing happens. What do I do?
#Import arcpy import arcpy import os #Naming the directory rootdir = "C:\\Users\\Fred\\Desktop\\GIS\\ModelBuilder Project\\TEST" #allowing overwriting arcpy.env.overwriteOutput = True #Setting up counter thectr = 0 #For-loop for looping function over all files in a folder for subdirs, dirs, files in os.walk(rootdir): for file in files: if file == '*.shp': # Variables input_layer = files output_layer = "C:\\Users\\Fred\\Desktop\\GIS\\ModelBuilder Project\\TEST\\test" + str(thectr) # Process: Buffer arcpy.Buffer_analysis(input_layer, output_layer, "1 Kilometers", "FULL", "ROUND", "ALL", "") #Update counter thectr=thectr+1
I've also tried the script below, with the same result:
# import modules import arcpy, glob # folder where shapefiles are stored folder = 'C:/myfiles/' # loop through all shapefiles for shapefile in glob.glob( folder + '*.shp' ): arcpy.Buffer_analysis( shapefile, "C:/output/" + shapefile[-8:-4] + "buffer.shp" "100 Feet", "FULL", "ROUND", "LIST", "Distance")
Ideas?
The output file name needs a '.shp' maybe?
output_layer = "C:\\Users\\Fred\\Desktop\\GIS\\ModelBuilder Project\\TEST\\test" + str(thectr) +".shp"
Still not working =/
Thanks though.
In your second script (the glob script), what is the output of "print shapefile' inside the loop? Does it list anything? If so, do you notice anything out of the ordinary?
You're setting input_layer to files. files is a list, not a shapefile or layer.
Also, don't think 'if file = '*.shp' will work. The * isn't working as a wildcard in this case, so most likely you're never entering the if block. This is also indicated by the fact that no error is being thrown. That code is never run.
You could try: if file.endswith('.shp) instead. Or import fname and use: if fname.fname('*.shp').
I'd also suggest putting in some print statements so you can see what values you're getting and where in the code you are.
#For-loop for looping function over all files in a folder for file in files: if file.endswith('.shp'): print('File name is: {0}'.format(file) # Variables output_layer = "C:\\Users\\Fred\\Desktop\\GIS\\ModelBuilder Project\\TEST\\test" + str(thectr) arcpy.Buffer_analysis(file, output_layer, "1 Kilometers", "FULL", "ROUND", "ALL", "")
This embellishment on your second (glob) script works for me. It saves the shapefile with the same name to a test folder. You can easily add a suffix, if you want:
>>> import glob, os >>> folder = r'C:/junk/' >>> for shapefile in glob.glob( folder + '*.shp' ): ... output = os.path.join(r'C:/junk/test' + os.path.basename(shapefile)) ... arcpy.Buffer_analysis( shapefile, output, '100 METERS')
A combo of Greg's and Darren's answer worked for me:
import arcpy, os, string rootFolder = 'C:\\test' ctr = 1 for root, dirs, files in os.walk(rootFolder): for name in files: if name.endswith(".shp"): # shapefile name without extension shpName = os.path.splitext(name)[0] # absolute file path absFile = os.path.abspath(os.path.join(root,name)) # output file path output_layer = rootFolder + '\\buffer_' + shpName + '_' + str(ctr) + '.shp' # buffer arcpy.Buffer_analysis(absFile, output_layer, "1 Kilometers", "FULL", "ROUND", "ALL", "") ctr = ctr + 1
Just for funzies, if you are using a simple counter like you do at the end with ctr, you can also use += 1 to make the code a little simpler. Just know that += might not always be what you want.
ctr += 1
I would try the arcpy.ListFeatureClasses() to get a list of shape file in folder.
import os import arcpy # Set the workspace for ListFeatureClasses dirpath = "C:/Users/Fred/Desktop/GIS/ModelBuilder Project/TEST" arcpy.env.workspace = dirpath # Use the ListFeatureClasses function to return a list of shapefiles. featureclasses = arcpy.ListFeatureClasses() # loop through the list of shape files for ifc in featureclasses: ofc = os.path.join(dirpath,os.path.splitext(ifc)[0] + "_Buffer.shp") arcpy.Buffer_analysis(ifc,ofc,"1 Kilometer","FULL","ROUND","ALL","")
I'm doing the same type of thing except with an append. I pass in the folder as an argument.
# Get list of shape files from specified directory
path = sys.argv[1] + "\\*.shp"
directory = glob.glob(path)
for filename in directory:
try:
arcpy.Append_management(filename,sys.argv[4], "NO_TEST", "SHAPE.AREA \"SHAPE.AREA\" false false true 0 Double 0 0 ,First,#;SHAPE.LEN \"SHAPE.LEN\" false false true 0 Double 0 0 ,First,#", "")
except:
arcpy.AddMessage(traceback.format_exc())
error = "Y"