I'm having a bad time trying to create a script capable of merging a list of arcinfo coverages located inside a folder structure. My goal is to merge the coverages named "car" that are inside each of 500 folders. The field definitions are the same for all the "car" coverages.

My unsuccessful approach has been first to create a list with the path of each folder and second apply the arcpy.ListDatasets function to each of the folders and append (or merge) each of the coverages to an initial empty shapefile.

I'm new in Python and a simple task like this has become hours of trial and error. I hope someone here can guide me through the best solution.

This is the code I have so far (with indentation error on line 21):

import arcpy
from arcpy import env
import os

outlocation = "W:/forestal/1_25k_carreteras"
folderspath = "W:/forestal/cartografia/hojas_1_25k"
emptyshp = "1_25k_car.shp"
folderslst = []

arcpy.CreateFeatureclass_management(outlocation, emptyshp, "POLYLINE")

for dirname, dirnames, filenames in os.walk(folderspath):
    for subdirname in dirnames:

for folder in folderslst:
        arcpy.env.workspace = folder
        coverageslst = arcpy.ListDatasets("car", "Coverage")
        arcpy.Append_management(coverageslst, outLocation + os.sep + emptyshp)


  • are the files you are looking to merge named "car" exactly, or do they just have "car" in the name? also, why the nested for loop in the first for loop? – ndimhypervol Feb 21 '14 at 4:37
  • these are arcInfo coverages, and yes.. they are named "car" – Guillermo.D Feb 21 '14 at 5:48
  • I copied the nested for loop from a script another person did for a similar task. I get everything right until the second for loop. I get an indentation error in the coverageslst = arcpy.ListDatasets("car", "Coverage") section which I still don't understand why. – Guillermo.D Feb 21 '14 at 5:51
  • As rendered, your code has an indentation error in line after for folder in folderslst: as the next line has 8 spaces rather than 4 spaces you have followed until that line – RedBaron Feb 21 '14 at 5:55
  • oops! you are absolutely right RedBaron... thanks a lot for that! – Guillermo.D Feb 21 '14 at 5:58

The following script will walk through a directory and create a list of of all of the files containing "car" in the filename. Once you have the list, use it as an input for the merge function.

import arcpy

ws = r'C:\temp'

# Get a list of all "car" files in subfolders
coverages = []
for dirpath, dirnames, filenames in arcpy.da.Walk(ws, topdown = True):
    for filename in filenames:
        if "car" in filename:
            coverages.append(os.path.join(dirpath, filename))

# Do something with the coverage list
| improve this answer | |

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.