<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: go through list of shapefiles and delete all but required fields - arcpy in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/go-through-list-of-shapefiles-and-delete-all-but/m-p/408824#M32202</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I probably would not use the Merge tool for this. Especially if you are not interesting in the attributes (only geometry). You could probably do something like this. Please not that I did not run the code, so be careful...&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;... but if it works it will:&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;create an empty output featureclass (which should not be written to the folder that is being searched)&lt;/LI&gt;&lt;LI&gt;searches for featureclasses of type polygon in the folder and all subfolders&lt;/LI&gt;&lt;LI&gt;use a search cursor to obtain the geometry and insert it in the output featureclass&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;import arcpy
import os
arcpy.env.overwriteOutput = True

# settings
basefolder = r"C:\Forum"
fc_out = r"C:\Data\myFGDB.gdb\myResultingFeatureclass"
sr = arcpy.SpatialReference(102100) # specify the correct WKID!

# create the empty output fc (outside the basefolder!)
fc_ws, fc_name = os.path.split(fc_out)
arcpy.CreateFeatureclass_management(fc_ws, fc_name, "POLYGON", spatial_reference=sr)

cnt = 0
# start a da insert cursor
with arcpy.da.InsertCursor(fc_out, ("SHAPE@")) as curs_out:

&amp;nbsp;&amp;nbsp;&amp;nbsp; # create a list of all polygon featureclasses in (sub)folders of the basefolder
&amp;nbsp;&amp;nbsp;&amp;nbsp; walk = arcpy.da.Walk(basefolder, datatype="FeatureClass", type="Polygon")

&amp;nbsp;&amp;nbsp;&amp;nbsp; # loop through featureclasses
&amp;nbsp;&amp;nbsp;&amp;nbsp; for dirpath, dirnames, filenames in walk:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; for filename in filenames:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # construct the absolute name to input featureclass
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; fc_in = os.path.join(dirpath, filename)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # for shapefiles (but also coverages)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; desc = arcpy.Describe(dirpath)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if hasattr(desc, "workspaceType"):
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if arcpy.Describe(dirpath).workspaceType == "FileSystem":

&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # start search cursor and insert features into output featureclass
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print "Processing featureclass: '{0}'".format(filename)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; with arcpy.da.SearchCursor(fc_in, ("SHAPE@")) as curs_in:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; for row in curs_in:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; cnt += 1
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if cnt % 1000 == 0:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print " - Inserting output feature: {0}".format(cnt)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; curs_out.insertRow((row[0], ))

del curs_out, curs_in, row&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;A simple enhancement could be to add a field to the output featureclass (text) and write the source shapefile name to the field. &lt;/P&gt;&lt;P&gt;There is no check to see if the coordinate system coincides with the output featureclass.&lt;/P&gt;&lt;P&gt;Also other polygon featureclasses like coverages, if present, will be included in the result.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Sat, 11 Dec 2021 18:34:51 GMT</pubDate>
    <dc:creator>XanderBakker</dc:creator>
    <dc:date>2021-12-11T18:34:51Z</dc:date>
    <item>
      <title>go through list of shapefiles and delete all but required fields - arcpy</title>
      <link>https://community.esri.com/t5/python-questions/go-through-list-of-shapefiles-and-delete-all-but/m-p/408820#M32198</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I have about 1000 shapefiles that I will be merging into one large shapefile. I am not interested in the attribute information, only the location. I was thinking I should try to condense the files before merging by stripping out all the fields except the required ones and them merging them all together, All the files should be polygons but a few non polygons may creep in so a check for file type would probably be nice. (I think I can code this requirement on my own).&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Right now all the files are spread out in multiple directories but I have the file names and directory path listed in a txt file. I would want to copy all these files to a single location, delete the unnecessary fields and then merge. I want to do this all at once instead of each file individually. I know how to code this individually but am unsure how to accomplish this with such a large volume of data. Ideas anyone?&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;ver. ArcGIS 10.2.1 Advanced&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 28 Jan 2015 22:44:01 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/go-through-list-of-shapefiles-and-delete-all-but/m-p/408820#M32198</guid>
      <dc:creator>BrendaJameson</dc:creator>
      <dc:date>2015-01-28T22:44:01Z</dc:date>
    </item>
    <item>
      <title>Re: go through list of shapefiles and delete all but required fields - arcpy</title>
      <link>https://community.esri.com/t5/python-questions/go-through-list-of-shapefiles-and-delete-all-but/m-p/408821#M32199</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi Brenda.&lt;/P&gt;&lt;P&gt;If I were you, I would work with lists.&lt;/P&gt;&lt;P&gt;One list for all your shapefiles (shps = ["path/to/1","Path/to/2",...])&lt;/P&gt;&lt;P&gt;And one list for the fields you do not want to delete (flds2keep = ["field1","field2",...]) Don't forget to add OID and SHAPE field.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Then for each shapefile, copy the shapefile in one directory and buil a list whith the new shapefiles&lt;/P&gt;&lt;P&gt;For each new shapefile, for each field, if it is not in the list, delete it.&lt;/P&gt;&lt;P&gt;When this is finished, merge all the new shapefiles in one.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 28 Jan 2015 22:56:43 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/go-through-list-of-shapefiles-and-delete-all-but/m-p/408821#M32199</guid>
      <dc:creator>JérémiePedoia</dc:creator>
      <dc:date>2015-01-28T22:56:43Z</dc:date>
    </item>
    <item>
      <title>Re: go through list of shapefiles and delete all but required fields - arcpy</title>
      <link>https://community.esri.com/t5/python-questions/go-through-list-of-shapefiles-and-delete-all-but/m-p/408822#M32200</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;iterate through each directory + shapefile in text file (hopefully paths in the correct format)&lt;BR /&gt;if statement for shape type&lt;BR /&gt;if not statement for deleting fields except OID and shape&lt;BR /&gt;merge all shapefiles into first (or create a new one merge into that)&lt;/P&gt;&lt;P&gt;unless you really want them in 1 directory don't need to copy or you can add a copy in&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 28 Jan 2015 23:42:22 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/go-through-list-of-shapefiles-and-delete-all-but/m-p/408822#M32200</guid>
      <dc:creator>AmyKlug</dc:creator>
      <dc:date>2015-01-28T23:42:22Z</dc:date>
    </item>
    <item>
      <title>Re: go through list of shapefiles and delete all but required fields - arcpy</title>
      <link>https://community.esri.com/t5/python-questions/go-through-list-of-shapefiles-and-delete-all-but/m-p/408823#M32201</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Since you already have the paths/filenames in a text file, I would read each line and use os.path.join to append them to the fullpath/filename and append to a list.&amp;nbsp; Personally, I'd make a separate script for this just to copy them all to a common location, then would run the delete/merge script, but could be included if this part might be repeated in the future.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Now that they are in a common folder (and copies as you don't want to delete on your only copy), first, set arcpy.env.workspace = "path to new common folder", then use list files to grab all the *.shp files in the new folder. &lt;SPAN style="font-family: 'Calibri',sans-serif; font-size: 11pt; mso-ascii-theme-font: minor-latin; mso-fareast-font-family: Calibri; mso-fareast-theme-font: minor-latin; mso-hansi-theme-font: minor-latin; mso-bidi-font-family: 'Times New Roman'; mso-bidi-theme-font: minor-bidi; mso-ansi-language: EN-US; mso-fareast-language: EN-US; mso-bidi-language: AR-SA;"&gt;&lt;A href="http://resources.arcgis.com/en/help/main/10.2/index.html#//03q300000018000000"&gt;http://resources.arcgis.com/en/help/main/10.2/index.html#//03q300000018000000&lt;/A&gt;&lt;BR /&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'Calibri',sans-serif; font-size: 11pt; mso-ascii-theme-font: minor-latin; mso-fareast-font-family: Calibri; mso-fareast-theme-font: minor-latin; mso-hansi-theme-font: minor-latin; mso-bidi-font-family: 'Times New Roman'; mso-bidi-theme-font: minor-bidi; mso-ansi-language: EN-US; mso-fareast-language: EN-US; mso-bidi-language: AR-SA;"&gt;(looks like you could use ListFeatureClasses &lt;SPAN style="font-family: Calibri;"&gt;&lt;A href="http://resources.arcgis.com/en/help/main/10.2/index.html#//03q300000023000000"&gt;http://resources.arcgis.com/en/help/main/10.2/index.html#//03q300000023000000&lt;/A&gt;&lt;/SPAN&gt; to make the list as well.&amp;nbsp;&amp;nbsp; Probably better as you can filter to only shapefiles, poly and/or point in one swoop something like &lt;SPAN style="font-family: Calibri;"&gt;fcList = arcpy.ListFeatureClasses("*.shp",["Poylgon", "Point"]) which will make a list of just point and/or polygon shapefiles in the current workspace)&amp;nbsp; However, in either case, you won't be able to merge different geometry types, so won't be able to merge points will polys, so could run one iteration with ListFeatureClasses just grabbing points, then run again with the polygons.&amp;nbsp; Will have to account for this in the merge output filename so it doesn't clobber one with the other.&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;then iterate trough the list and send to list fields &lt;SPAN style="font-family: 'Calibri',sans-serif; font-size: 11pt; mso-ascii-theme-font: minor-latin; mso-fareast-font-family: Calibri; mso-fareast-theme-font: minor-latin; mso-hansi-theme-font: minor-latin; mso-bidi-font-family: 'Times New Roman'; mso-bidi-theme-font: minor-bidi; mso-ansi-language: EN-US; mso-fareast-language: EN-US; mso-bidi-language: AR-SA;"&gt;&lt;A href="http://resources.arcgis.com/en/help/main/10.2/index.html#//018v00000012000000"&gt;http://resources.arcgis.com/en/help/main/10.2/index.html#//018v00000012000000&lt;/A&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;inside this loop, iterate trough the fields, and append to new fieldList if not "Shape" or "FID" (FID since shapefiles, if other input format, might be OBJECTID).&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Now, since the field list is complete for the current shapefile, pass it to delete fields (which will take the list directly, no need to iterate) &lt;SPAN style="font-family: 'Calibri',sans-serif; font-size: 11pt; mso-ascii-theme-font: minor-latin; mso-fareast-font-family: Calibri; mso-fareast-theme-font: minor-latin; mso-hansi-theme-font: minor-latin; mso-bidi-font-family: 'Times New Roman'; mso-bidi-theme-font: minor-bidi; mso-ansi-language: EN-US; mso-fareast-language: EN-US; mso-bidi-language: AR-SA;"&gt;&lt;A href="http://resources.arcgis.com/en/help/main/10.2/index.html#//00170000004n000000"&gt;http://resources.arcgis.com/en/help/main/10.2/index.html#//00170000004n000000&lt;/A&gt; to delete them.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'Calibri',sans-serif; font-size: 11pt; mso-ascii-theme-font: minor-latin; mso-fareast-font-family: Calibri; mso-fareast-theme-font: minor-latin; mso-hansi-theme-font: minor-latin; mso-bidi-font-family: 'Times New Roman'; mso-bidi-theme-font: minor-bidi; mso-ansi-language: EN-US; mso-fareast-language: EN-US; mso-bidi-language: AR-SA;"&gt;set the fieldList = []&amp;nbsp; - so is empty for the next shapefile loop.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'Calibri',sans-serif; font-size: 11pt; mso-ascii-theme-font: minor-latin; mso-fareast-font-family: Calibri; mso-fareast-theme-font: minor-latin; mso-hansi-theme-font: minor-latin; mso-bidi-font-family: 'Times New Roman'; mso-bidi-theme-font: minor-bidi; mso-ansi-language: EN-US; mso-fareast-language: EN-US; mso-bidi-language: AR-SA;"&gt;This will remove all but the FID and Shape fields from each shapefile.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'Calibri',sans-serif; font-size: 11pt; mso-ascii-theme-font: minor-latin; mso-fareast-font-family: Calibri; mso-fareast-theme-font: minor-latin; mso-hansi-theme-font: minor-latin; mso-bidi-font-family: 'Times New Roman'; mso-bidi-theme-font: minor-bidi; mso-ansi-language: EN-US; mso-fareast-language: EN-US; mso-bidi-language: AR-SA;"&gt;You still have the list of files from ListFIles, just pass this list directly to merge &lt;SPAN style="font-family: 'Calibri',sans-serif; font-size: 11pt; mso-ascii-theme-font: minor-latin; mso-fareast-font-family: Calibri; mso-fareast-theme-font: minor-latin; mso-hansi-theme-font: minor-latin; mso-bidi-font-family: 'Times New Roman'; mso-bidi-theme-font: minor-bidi; mso-ansi-language: EN-US; mso-fareast-language: EN-US; mso-bidi-language: AR-SA;"&gt;&lt;A href="http://resources.arcgis.com/en/help/main/10.2/index.html#//001700000055000000"&gt;http://resources.arcgis.com/en/help/main/10.2/index.html#//001700000055000000&lt;/A&gt;&lt;/SPAN&gt; with a new output filename and run it.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'Calibri',sans-serif; font-size: 11pt; mso-ascii-theme-font: minor-latin; mso-fareast-font-family: Calibri; mso-fareast-theme-font: minor-latin; mso-hansi-theme-font: minor-latin; mso-bidi-font-family: 'Times New Roman'; mso-bidi-theme-font: minor-bidi; mso-ansi-language: EN-US; mso-fareast-language: EN-US; mso-bidi-language: AR-SA;"&gt;R_&lt;/SPAN&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 29 Jan 2015 02:05:35 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/go-through-list-of-shapefiles-and-delete-all-but/m-p/408823#M32201</guid>
      <dc:creator>RhettZufelt</dc:creator>
      <dc:date>2015-01-29T02:05:35Z</dc:date>
    </item>
    <item>
      <title>Re: go through list of shapefiles and delete all but required fields - arcpy</title>
      <link>https://community.esri.com/t5/python-questions/go-through-list-of-shapefiles-and-delete-all-but/m-p/408824#M32202</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I probably would not use the Merge tool for this. Especially if you are not interesting in the attributes (only geometry). You could probably do something like this. Please not that I did not run the code, so be careful...&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;... but if it works it will:&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;create an empty output featureclass (which should not be written to the folder that is being searched)&lt;/LI&gt;&lt;LI&gt;searches for featureclasses of type polygon in the folder and all subfolders&lt;/LI&gt;&lt;LI&gt;use a search cursor to obtain the geometry and insert it in the output featureclass&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;import arcpy
import os
arcpy.env.overwriteOutput = True

# settings
basefolder = r"C:\Forum"
fc_out = r"C:\Data\myFGDB.gdb\myResultingFeatureclass"
sr = arcpy.SpatialReference(102100) # specify the correct WKID!

# create the empty output fc (outside the basefolder!)
fc_ws, fc_name = os.path.split(fc_out)
arcpy.CreateFeatureclass_management(fc_ws, fc_name, "POLYGON", spatial_reference=sr)

cnt = 0
# start a da insert cursor
with arcpy.da.InsertCursor(fc_out, ("SHAPE@")) as curs_out:

&amp;nbsp;&amp;nbsp;&amp;nbsp; # create a list of all polygon featureclasses in (sub)folders of the basefolder
&amp;nbsp;&amp;nbsp;&amp;nbsp; walk = arcpy.da.Walk(basefolder, datatype="FeatureClass", type="Polygon")

&amp;nbsp;&amp;nbsp;&amp;nbsp; # loop through featureclasses
&amp;nbsp;&amp;nbsp;&amp;nbsp; for dirpath, dirnames, filenames in walk:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; for filename in filenames:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # construct the absolute name to input featureclass
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; fc_in = os.path.join(dirpath, filename)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # for shapefiles (but also coverages)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; desc = arcpy.Describe(dirpath)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if hasattr(desc, "workspaceType"):
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if arcpy.Describe(dirpath).workspaceType == "FileSystem":

&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # start search cursor and insert features into output featureclass
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print "Processing featureclass: '{0}'".format(filename)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; with arcpy.da.SearchCursor(fc_in, ("SHAPE@")) as curs_in:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; for row in curs_in:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; cnt += 1
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if cnt % 1000 == 0:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print " - Inserting output feature: {0}".format(cnt)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; curs_out.insertRow((row[0], ))

del curs_out, curs_in, row&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;A simple enhancement could be to add a field to the output featureclass (text) and write the source shapefile name to the field. &lt;/P&gt;&lt;P&gt;There is no check to see if the coordinate system coincides with the output featureclass.&lt;/P&gt;&lt;P&gt;Also other polygon featureclasses like coverages, if present, will be included in the result.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 18:34:51 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/go-through-list-of-shapefiles-and-delete-all-but/m-p/408824#M32202</guid>
      <dc:creator>XanderBakker</dc:creator>
      <dc:date>2021-12-11T18:34:51Z</dc:date>
    </item>
    <item>
      <title>Re: go through list of shapefiles and delete all but required fields - arcpy</title>
      <link>https://community.esri.com/t5/python-questions/go-through-list-of-shapefiles-and-delete-all-but/m-p/408825#M32203</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;This worked great once I figured out I have to create the FGDB. LOL.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 31 Jan 2015 03:15:07 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/go-through-list-of-shapefiles-and-delete-all-but/m-p/408825#M32203</guid>
      <dc:creator>BrendaJameson</dc:creator>
      <dc:date>2015-01-31T03:15:07Z</dc:date>
    </item>
    <item>
      <title>Re: go through list of shapefiles and delete all but required fields - arcpy</title>
      <link>https://community.esri.com/t5/python-questions/go-through-list-of-shapefiles-and-delete-all-but/m-p/408826#M32204</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;sorry, that could have been clearer.... &lt;IMG src="https://community.esri.com/legacyfs/online/emoticons/wink.png" /&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 31 Jan 2015 17:58:34 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/go-through-list-of-shapefiles-and-delete-all-but/m-p/408826#M32204</guid>
      <dc:creator>XanderBakker</dc:creator>
      <dc:date>2015-01-31T17:58:34Z</dc:date>
    </item>
    <item>
      <title>Re: go through list of shapefiles and delete all but required fields - arcpy</title>
      <link>https://community.esri.com/t5/python-questions/go-through-list-of-shapefiles-and-delete-all-but/m-p/408827#M32205</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;How would I go about modifying script to put the file name in a separate field? I had a script that did this before merging but I keep getting [Errno 10054] An existing connection was forcibly closed by the remote host when run. I was hoping I could accomplish by modifying your script.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Many thanks,&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 02 Feb 2015 19:13:30 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/go-through-list-of-shapefiles-and-delete-all-but/m-p/408827#M32205</guid>
      <dc:creator>BrendaJameson</dc:creator>
      <dc:date>2015-02-02T19:13:30Z</dc:date>
    </item>
    <item>
      <title>Re: go through list of shapefiles and delete all but required fields - arcpy</title>
      <link>https://community.esri.com/t5/python-questions/go-through-list-of-shapefiles-and-delete-all-but/m-p/408828#M32206</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I haven't tested this, but the code would change just a little:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;line 8 the name of the output field that contains the source featureclass&lt;/LI&gt;&lt;LI&gt;line 16 add the field to the output featureclass&lt;/LI&gt;&lt;LI&gt;line 42, include the path+name of the source in the tuple (output row)&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;import arcpy
import os
arcpy.env.overwriteOutput = True

# settings
basefolder = r"C:\Forum"
fc_out = r"C:\Data\myFGDB.gdb\myResultingFeatureclass"
fld_source = "SourceFC"
sr = arcpy.SpatialReference(102100) # specify the correct WKID!

# create the empty output fc (outside the basefolder!)
fc_ws, fc_name = os.path.split(fc_out)
arcpy.CreateFeatureclass_management(fc_ws, fc_name, "POLYGON", spatial_reference=sr)

# add the field that will contain the source:
arcpy.AddField_management(fc_out, fld_source, "TEXT", field_length=255)

cnt = 0
# start a da insert cursor
with arcpy.da.InsertCursor(fc_out, ("SHAPE@", fld_source)) as curs_out:

&amp;nbsp;&amp;nbsp;&amp;nbsp; # create a list of all polygon featureclasses in (sub)folders of the basefolder
&amp;nbsp;&amp;nbsp;&amp;nbsp; walk = arcpy.da.Walk(basefolder, datatype="FeatureClass", type="Polygon")

&amp;nbsp;&amp;nbsp;&amp;nbsp; # loop through featureclasses
&amp;nbsp;&amp;nbsp;&amp;nbsp; for dirpath, dirnames, filenames in walk:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; for filename in filenames:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # construct the absolute name to input featureclass
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; fc_in = os.path.join(dirpath, filename)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # for shapefiles (but also coverages)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; desc = arcpy.Describe(dirpath)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if hasattr(desc, "workspaceType"):
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if arcpy.Describe(dirpath).workspaceType == "FileSystem":

&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # start search cursor and insert features into output featureclass
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print "Processing featureclass: '{0}'".format(filename)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; with arcpy.da.SearchCursor(fc_in, ("SHAPE@")) as curs_in:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; for row in curs_in:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; cnt += 1
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if cnt % 1000 == 0:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print " - Inserting output feature: {0}".format(cnt)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; curs_out.insertRow((row[0], fc_in, ))

del curs_out, curs_in, row&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 18:34:54 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/go-through-list-of-shapefiles-and-delete-all-but/m-p/408828#M32206</guid>
      <dc:creator>XanderBakker</dc:creator>
      <dc:date>2021-12-11T18:34:54Z</dc:date>
    </item>
    <item>
      <title>Re: go through list of shapefiles and delete all but required fields - arcpy</title>
      <link>https://community.esri.com/t5/python-questions/go-through-list-of-shapefiles-and-delete-all-but/m-p/408829#M32207</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Well that was just too awesome for words. Thank you a gajillion times.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 02 Feb 2015 21:43:10 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/go-through-list-of-shapefiles-and-delete-all-but/m-p/408829#M32207</guid>
      <dc:creator>BrendaJameson</dc:creator>
      <dc:date>2015-02-02T21:43:10Z</dc:date>
    </item>
    <item>
      <title>Re: go through list of shapefiles and delete all but required fields - arcpy</title>
      <link>https://community.esri.com/t5/python-questions/go-through-list-of-shapefiles-and-delete-all-but/m-p/408830#M32208</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I think I have a simple approach: create a empty shapefile in the coordinate system you want and use the Append tool with NO_TEST. This will ignore all fields that do not have a name match to the output shapefile.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 02 Feb 2015 21:45:08 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/go-through-list-of-shapefiles-and-delete-all-but/m-p/408830#M32208</guid>
      <dc:creator>curtvprice</dc:creator>
      <dc:date>2015-02-02T21:45:08Z</dc:date>
    </item>
  </channel>
</rss>

