<?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: Add parent directory name to output feature class with os walk in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/add-parent-directory-name-to-output-feature-class/m-p/1205290#M65383</link>
    <description>&lt;P&gt;With the os.path module:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;walk = arcpy.da.Walk(workspace, datatype = "FeatureClass")
for dirpath, dirnames, filenames in walk:
    for file in filenames:
        infc = os.path.join(dirpath, file) 
        outfcname = os.path.basename(file).split(".")[0] + "_" + os.path.basename(dirpath)
        outfc = os.path.join(outgdb, outfcname)
        arcpy.management.CopyFeatures(infc, outfc)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;With the newer pathlib module:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;from pathlib import Path

walk = arcpy.da.Walk(workspace, datatype = "FeatureClass")
for dirpath, dirnames, filenames in walk:
    for file in filenames:
        infc = Path(dirpath, file) 
        outfcname = Path(file).stem + "_" + Path(dirpath).stem
        print(outfcname)
        outfc = Path(outgdb, outfcname)
        arcpy.management.CopyFeatures(str(infc), str(outfc))  # arcpy is old and doesn't know about Path&lt;/LI-CODE&gt;</description>
    <pubDate>Tue, 23 Aug 2022 07:39:40 GMT</pubDate>
    <dc:creator>JohannesLindner</dc:creator>
    <dc:date>2022-08-23T07:39:40Z</dc:date>
    <item>
      <title>Add parent directory name to output feature class with os walk</title>
      <link>https://community.esri.com/t5/python-questions/add-parent-directory-name-to-output-feature-class/m-p/1205263#M65382</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have several folders under one big folder called "Counties". I'm attempting to use arcpy.da.walk to copy features. I'm trying to copy a shapefile called "Parcels" from each folder within the Counties folder. I run the code below and it works on one county folder, but then stops because the name of the shapefile is the same. So I now want to rename them in the same code block. Is this possble? I'd like to attach the name of the parent directory (which is the county name) so the output fc is "Parcels_Anderson", "Parcels_Bedford", etc.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any help appreciated!&lt;/P&gt;&lt;P&gt;Here is the code:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;workspace = "D:/Geodata/Cadastral/Parcels/Counties"
outgdb = "D:/Geodata/Cadastral/Parcels/Parcels_features.gdb"
walk = arcpy.da.Walk(workspace, datatype = "FeatureClass")
for dirpath, dirnames, filenames in walk:
    for file in filenames:
        infc = os.path.join(dirpath, file) 
        desc = arcpy.da.Describe(infc)
        outfc = os.path.join(outgdb, desc["baseName"])
        arcpy.management.CopyFeatures(infc, outfc)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 23 Aug 2022 03:32:02 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/add-parent-directory-name-to-output-feature-class/m-p/1205263#M65382</guid>
      <dc:creator>KyleSmiley</dc:creator>
      <dc:date>2022-08-23T03:32:02Z</dc:date>
    </item>
    <item>
      <title>Re: Add parent directory name to output feature class with os walk</title>
      <link>https://community.esri.com/t5/python-questions/add-parent-directory-name-to-output-feature-class/m-p/1205290#M65383</link>
      <description>&lt;P&gt;With the os.path module:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;walk = arcpy.da.Walk(workspace, datatype = "FeatureClass")
for dirpath, dirnames, filenames in walk:
    for file in filenames:
        infc = os.path.join(dirpath, file) 
        outfcname = os.path.basename(file).split(".")[0] + "_" + os.path.basename(dirpath)
        outfc = os.path.join(outgdb, outfcname)
        arcpy.management.CopyFeatures(infc, outfc)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;With the newer pathlib module:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;from pathlib import Path

walk = arcpy.da.Walk(workspace, datatype = "FeatureClass")
for dirpath, dirnames, filenames in walk:
    for file in filenames:
        infc = Path(dirpath, file) 
        outfcname = Path(file).stem + "_" + Path(dirpath).stem
        print(outfcname)
        outfc = Path(outgdb, outfcname)
        arcpy.management.CopyFeatures(str(infc), str(outfc))  # arcpy is old and doesn't know about Path&lt;/LI-CODE&gt;</description>
      <pubDate>Tue, 23 Aug 2022 07:39:40 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/add-parent-directory-name-to-output-feature-class/m-p/1205290#M65383</guid>
      <dc:creator>JohannesLindner</dc:creator>
      <dc:date>2022-08-23T07:39:40Z</dc:date>
    </item>
    <item>
      <title>Re: Add parent directory name to output feature class with os walk</title>
      <link>https://community.esri.com/t5/python-questions/add-parent-directory-name-to-output-feature-class/m-p/1205327#M65385</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you for that! I had one last thing I was trying to do and forgot to mention. In addition to above, is there a way to walk for a specific string?&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;maybe something like:&lt;/P&gt;&lt;P&gt;for file in filenames:&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;for “Pacels” in filenames:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;or&amp;nbsp;&lt;/P&gt;&lt;P&gt;If filenames is “Parcels”&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;thanks in advance&lt;/P&gt;&lt;P&gt;&amp;nbsp;and so on….&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 23 Aug 2022 11:40:31 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/add-parent-directory-name-to-output-feature-class/m-p/1205327#M65385</guid>
      <dc:creator>KyleSmiley</dc:creator>
      <dc:date>2022-08-23T11:40:31Z</dc:date>
    </item>
    <item>
      <title>Re: Add parent directory name to output feature class with os walk</title>
      <link>https://community.esri.com/t5/python-questions/add-parent-directory-name-to-output-feature-class/m-p/1205340#M65386</link>
      <description>&lt;LI-CODE lang="python"&gt;# if filename isn't equal to "Parcels.shp", go to next file
for dirpath, dirnames, filenames in walk:
    for file in filenames:
        if file != "Parcels.shp":
            continue
        infc = os.path.join(dirpath, file) 
        #...

# if filename doesn't start with "Parcels", go to next file
for dirpath, dirnames, filenames in walk:
    for file in filenames:
        if not file.startswith("Parcels"):
            continue
        infc = os.path.join(dirpath, file) 
        #...

# if filename doesn't contain "Parcels", go to next file
for dirpath, dirnames, filenames in walk:
    for file in filenames:
        if not "Parcels" in file:
            continue
        infc = os.path.join(dirpath, file) 
        #...&lt;/LI-CODE&gt;</description>
      <pubDate>Tue, 23 Aug 2022 12:14:45 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/add-parent-directory-name-to-output-feature-class/m-p/1205340#M65386</guid>
      <dc:creator>JohannesLindner</dc:creator>
      <dc:date>2022-08-23T12:14:45Z</dc:date>
    </item>
    <item>
      <title>Re: Add parent directory name to output feature class with os walk</title>
      <link>https://community.esri.com/t5/python-questions/add-parent-directory-name-to-output-feature-class/m-p/1205370#M65387</link>
      <description>&lt;P&gt;Good morning,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;thanks a ton! It worked!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Kyle&lt;/P&gt;</description>
      <pubDate>Tue, 23 Aug 2022 13:53:41 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/add-parent-directory-name-to-output-feature-class/m-p/1205370#M65387</guid>
      <dc:creator>KyleSmiley</dc:creator>
      <dc:date>2022-08-23T13:53:41Z</dc:date>
    </item>
  </channel>
</rss>

