I have a few hundred folders, each with a set of shapefiles that are identical across folders (ie each folder has a 'lot.shp'). They are all supposed to be polygon features, but unfortunately many are line features.

I am trying to convert all line features to polygons using Feature to Polygon.

To do this I have tried the following code which should work through each folder converting the desired shapefiles and saving back into the same folder but with a '2' in the name:

import arcpy, glob, os

filelist = ['buildingpoly','lot', etc etc]

path = "C:/folder_containing_desired_subfolders/*/"

for x in filelist:
    list = glob.glob(path + x + ".shp")
    arcpy.FeatureToPolygon_management(in_features=list, out_feature_class=path + x + "2.shp", cluster_tolerance="", attributes="ATTRIBUTES", label_features="")
    del list

But this gets the following error:

Runtime error Traceback (most recent call last): File "", line 10, in File "c:\program files (x86)\arcgis\desktop10.4\arcpy\arcpy\management.py", line 2524, in FeatureToPolygon raise e ExecuteError: ERROR 000210: Cannot create output C:/folder_containing_desired_subfolders/*/buildingpoly2.shp Failed to execute (FeatureToPolygon).

Any ideas? I'm at a loss on this one.

An alternative that would also work is if I can have all the new polygon features saved in the same directory but this would require each getting a unique name.


Here is code that works. It works through the folders and converts all polylines to polygons.

import os, sys, arcpy
directory = "C:/working_folder/"

for path, path_names, data_names in arcpy.da.Walk(directory, type="Polyline"):
    for data_name in data_names:
        arcpy.FeatureToPolygon_management(in_features= (os.path.join(path, data_name)), out_feature_class= (os.path.join(path, "two"+data_name)), cluster_tolerance="", attributes="ATTRIBUTES", label_features="")
  • 3
    The asterisk shouldn't appear in your path, consider arcpy.da.Walk() to find the lot.shp in each subfolder and arcpy.Describe shapeType == 'Polyline' to find the line lot.shp files to polygonize. Feb 24, 2019 at 23:26

1 Answer 1


Have a try with this code:

import os, sys, arcpy

path = "C:/folder_containing_desired_subfolders"
filelist = ['buildingpoly','lot', etc etc]

for (Spath,Sfiles,Sdirs) in arcpy.da.Walk(path):
    for ThisFile in Sfiles:
        # break up the file name and extension
        fName, fExt = os.path.splitext(ThisFile)
        if fExt.upper() == '.SHP':
            # only for .shp files
            if fName.lower() in filelist:
                # this fName is in the filelist of interest
                # get some info about the shapefile
                D = arcpy.Describe(os.path.join(Spath,ThisFile))
                if D.shapeType == 'Polyline':
                    # do your polygonization here
                elif D.shapeType == 'Polygon':
                    # do something for the already polygon feature classes

You say all your shapefiles are in subfolders so using arcpy.da.Walk() to traverse the folder tree, finding all Esri recognized feature classes and rasters then break up the file name and extension to filter by extension and then Describe to see if the feature class is a line or polygon.. this should get you a bit closer to where you're headed.

  • This works, except I can't get it to save the output file back into the same directory as the file it just worked on. So it will process the first one, save it to a location, then say it already exists and fails on the second one. Any suggestions for this?
    – Vince
    Feb 25, 2019 at 2:11
  • 1
    os.path.join(Spath,'<your file name>') will save the output in the folder the input is being read from; if you need to find all of them at the end start with an empty list and append each save path as it is created. The value of Spath changes to the workspace path of the list of file names returned in Sfiles. Feb 25, 2019 at 2:14
  • This also works. The one (big) issue now is that each newly created polygon feature is identical to whichever feature was first processed. It's naming them correctly and working through each folder correctly, but it's just processing the exact same feature over and over again and saving it as a different name + location each time. Is there some sort of cache that needs clearing between each process? I've tried arcpy.ClearWorkspaceCache_management() with no luck.
    – Vince
    Feb 25, 2019 at 4:17
  • I forgot to put the path into the walk(), had you already fixed that? If not specified it defaults to the arcpy.env.workspace. Please update your code in your question with what you have now and I'll see if I can find any glaring errors. Feb 25, 2019 at 5:22
  • 1
    I slept on it then read through it again and found my errors. Thanks! Will post my code.
    – Vince
    Feb 25, 2019 at 17:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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