<?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: Meger certain shapefiles within sub folders with path name in attributes in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/meger-certain-shapefiles-within-sub-folders-with/m-p/1051551#M60909</link>
    <description>&lt;P&gt;&lt;A href="https://pro.arcgis.com/en/pro-app/latest/tool-reference/data-management/add-field.htm" target="_blank"&gt;Add Field (Data Management)—ArcGIS Pro | Documentation&lt;/A&gt;&lt;/P&gt;&lt;P&gt;arcpy.management.AddField(&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;in_table,&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; field_name,&amp;nbsp; &amp;nbsp; # field, "Path",&amp;nbsp; &amp;nbsp;which is it?&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; field_type,&amp;nbsp; &amp;nbsp; &amp;nbsp; #&amp;nbsp; 'TEXT'&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; {field_precision}, {field_scale}, {field_length}, {field_alias}, {field_is_nullable}, {field_is_required}, {field_domain}&lt;/P&gt;&lt;P&gt;but putting "field" and "Path" in, you pushed "Text" into&amp;nbsp; the field precision place&lt;/P&gt;</description>
    <pubDate>Mon, 26 Apr 2021 22:46:37 GMT</pubDate>
    <dc:creator>DanPatterson</dc:creator>
    <dc:date>2021-04-26T22:46:37Z</dc:date>
    <item>
      <title>Meger certain shapefiles within sub folders with path name in attributes</title>
      <link>https://community.esri.com/t5/python-questions/meger-certain-shapefiles-within-sub-folders-with/m-p/1050398#M60870</link>
      <description>&lt;P&gt;I need to merge certain shapefiles with a specific name in sub directories. I can do the merge but where I am stuck on is including each shapefile specific root path to each shapefile 'Path' field before the merge. I have the following.&lt;/P&gt;&lt;P&gt;rootpath will like below, Shapefile.shp will always be the same&lt;BR /&gt;C:\Tem\blah\2020 blah\Case1\Shapefile.shp&lt;BR /&gt;C:\Tem\blah\2020 blah\Case2\Shapefile.shp&lt;BR /&gt;C:\Tem\blah\2020 blah\Notice\Shapefile.shp&lt;BR /&gt;C:\Tem\blah\2020 blah\Use\Shapefile.shp&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;rootPath = r'C:\Temo\blah\2020 blah' # different folders aftre this
counter = 0
matches = []

for root, dirs, files in os.walk(rootPath):
    for filename in files:
        if filename == "shapefile.shp":
            match = ( os.path.join(root, filename))
            matches.append (match)
            counter = counter + 1

arcpy.Merge_management(matches, r"C:\Temp\MergedTest.shp")&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 22 Apr 2021 21:51:06 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/meger-certain-shapefiles-within-sub-folders-with/m-p/1050398#M60870</guid>
      <dc:creator>2Quiker</dc:creator>
      <dc:date>2021-04-22T21:51:06Z</dc:date>
    </item>
    <item>
      <title>Re: Meger certain shapefiles within sub folders with path name in attributes</title>
      <link>https://community.esri.com/t5/python-questions/meger-certain-shapefiles-within-sub-folders-with/m-p/1050415#M60871</link>
      <description>&lt;P&gt;add "dirs" into your loop then join root and dir, then that to filename&lt;/P&gt;&lt;P&gt;sort of like the second code example&lt;/P&gt;&lt;P&gt;&lt;A href="https://pro.arcgis.com/en/pro-app/latest/arcpy/data-access/walk.htm" target="_blank"&gt;Walk—ArcGIS Pro | Documentation&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 22 Apr 2021 22:28:59 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/meger-certain-shapefiles-within-sub-folders-with/m-p/1050415#M60871</guid>
      <dc:creator>DanPatterson</dc:creator>
      <dc:date>2021-04-22T22:28:59Z</dc:date>
    </item>
    <item>
      <title>Re: Meger certain shapefiles within sub folders with path name in attributes</title>
      <link>https://community.esri.com/t5/python-questions/meger-certain-shapefiles-within-sub-folders-with/m-p/1050691#M60886</link>
      <description>&lt;P&gt;I see but I am not sure how put it together,&amp;nbsp; do I put the "shapefile.shp" in a directory then&lt;SPAN class="usertext"&gt; use with arcpy.da.UpdateCursor to update the "Path" field with the dirnames? &lt;/SPAN&gt;&lt;SPAN class="usertext"&gt;I guess I am uncertain on how to put it together.&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 23 Apr 2021 14:51:25 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/meger-certain-shapefiles-within-sub-folders-with/m-p/1050691#M60886</guid>
      <dc:creator>2Quiker</dc:creator>
      <dc:date>2021-04-23T14:51:25Z</dc:date>
    </item>
    <item>
      <title>Re: Meger certain shapefiles within sub folders with path name in attributes</title>
      <link>https://community.esri.com/t5/python-questions/meger-certain-shapefiles-within-sub-folders-with/m-p/1051522#M60908</link>
      <description>&lt;P&gt;With the following I can get to work with one single shapefile to a specific folder location but not with a sub folder. It also only works if the "Path' field is already there. It won't let me add a field with line 13, ERROR 000622: Failed to execute (Add Field). Parameters are not valid.&lt;BR /&gt;ERROR 000628: Cannot set input into parameter field_precision.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;rootPath = r'C:\Temo\blah\2020 blah' # different folders aftre this
#rootPath = r'C:\Temo\blah\2020 blah\case\yoyo' #works with specific folder location
counter = 0
matches = []

field = 'Path'
for root, dirs, files in os.walk(rootPath):
    for filename in files:
        if filename == "shapefile.shp":
            match = ( os.path.join(root, filename))
            matches.append (match)
            counter = counter + 1

#arcpy.AddField_management(os.path.join(root,match),field, "Path", 'TEXT') #Erros with ERROR 000622: Failed to execute (Add Field). Parameters are not valid.
ERROR 000628: Cannot set input into parameter field_precision.
with arcpy.da.UpdateCursor(os.path.join(root,match),field) as cur:
    for row in cur:
        row[0]= rootPath
        cur.updateRow(row)
arcpy.Merge_management(matches, r"C:\Temp\MergedTest.shp")

 &lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 26 Apr 2021 21:35:08 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/meger-certain-shapefiles-within-sub-folders-with/m-p/1051522#M60908</guid>
      <dc:creator>2Quiker</dc:creator>
      <dc:date>2021-04-26T21:35:08Z</dc:date>
    </item>
    <item>
      <title>Re: Meger certain shapefiles within sub folders with path name in attributes</title>
      <link>https://community.esri.com/t5/python-questions/meger-certain-shapefiles-within-sub-folders-with/m-p/1051551#M60909</link>
      <description>&lt;P&gt;&lt;A href="https://pro.arcgis.com/en/pro-app/latest/tool-reference/data-management/add-field.htm" target="_blank"&gt;Add Field (Data Management)—ArcGIS Pro | Documentation&lt;/A&gt;&lt;/P&gt;&lt;P&gt;arcpy.management.AddField(&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;in_table,&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; field_name,&amp;nbsp; &amp;nbsp; # field, "Path",&amp;nbsp; &amp;nbsp;which is it?&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; field_type,&amp;nbsp; &amp;nbsp; &amp;nbsp; #&amp;nbsp; 'TEXT'&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; {field_precision}, {field_scale}, {field_length}, {field_alias}, {field_is_nullable}, {field_is_required}, {field_domain}&lt;/P&gt;&lt;P&gt;but putting "field" and "Path" in, you pushed "Text" into&amp;nbsp; the field precision place&lt;/P&gt;</description>
      <pubDate>Mon, 26 Apr 2021 22:46:37 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/meger-certain-shapefiles-within-sub-folders-with/m-p/1051551#M60909</guid>
      <dc:creator>DanPatterson</dc:creator>
      <dc:date>2021-04-26T22:46:37Z</dc:date>
    </item>
    <item>
      <title>Re: Meger certain shapefiles within sub folders with path name in attributes</title>
      <link>https://community.esri.com/t5/python-questions/meger-certain-shapefiles-within-sub-folders-with/m-p/1051763#M60917</link>
      <description>&lt;P&gt;Oh no, my bad I have been messing with it for a few hours and must have missed it.&lt;/P&gt;&lt;P&gt;&amp;nbsp;I have the updated code and I don't get an error but it doesn't populate all of the features 'Path' field and on the ones it does populate its not the path to shapefile, it is to another folder.&amp;nbsp; For example the a good path would be r'C:\Temo\blah\2020 blah\case\yoyo' but some of them are populated with r'C:\Temo\blah\2020 blah\case\Notifications' even though the shapefile is not in that 'Notifications' folder.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;rootPath = r'C:\Temo\blah\2020 blah' # different folders aftre this
#rootPath = r'C:\Temo\blah\2020 blah\case\yoyo' #works with specific folder location
counter = 0
matches = []

field = 'Path'
for root, dirs, files in os.walk(rootPath):
    for filename in files:
        if filename == "shapefile.shp":
            match = ( os.path.join(root, filename))
            matches.append (match)
            counter = counter + 1

arcpy.AddField_management(os.path.join(root,match),field, 'TEXT') 
with arcpy.da.UpdateCursor(os.path.join(root,match),field) as cur:
    for row in cur:
        row[0]= os.path.join(root, filename)
        cur.updateRow(row)
arcpy.Merge_management(matches, r"C:\Temp\MergedTest.shp")&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 27 Apr 2021 15:10:24 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/meger-certain-shapefiles-within-sub-folders-with/m-p/1051763#M60917</guid>
      <dc:creator>2Quiker</dc:creator>
      <dc:date>2021-04-27T15:10:24Z</dc:date>
    </item>
    <item>
      <title>Re: Meger certain shapefiles within sub folders with path name in attributes</title>
      <link>https://community.esri.com/t5/python-questions/meger-certain-shapefiles-within-sub-folders-with/m-p/1051816#M60919</link>
      <description>&lt;LI-CODE lang="python"&gt;print("you got = {}".format(filename))
if filename == "shapefile.shp":
    match = ( os.path.join(root, filename))
    matches.append (match)&lt;/LI-CODE&gt;&lt;P&gt;Are you reluctant to thrown in print statements to see what you got?&lt;/P&gt;&lt;P&gt;and this line&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;EM&gt;arcpy.AddField_management(os.path.join(root,match),field, 'TEXT')&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;will just use your last match and not cycle through your list of matches.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 27 Apr 2021 16:22:35 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/meger-certain-shapefiles-within-sub-folders-with/m-p/1051816#M60919</guid>
      <dc:creator>DanPatterson</dc:creator>
      <dc:date>2021-04-27T16:22:35Z</dc:date>
    </item>
    <item>
      <title>Re: Meger certain shapefiles within sub folders with path name in attributes</title>
      <link>https://community.esri.com/t5/python-questions/meger-certain-shapefiles-within-sub-folders-with/m-p/1052406#M60933</link>
      <description>&lt;P&gt;I was some what able to get what I need with the following.&lt;/P&gt;&lt;P&gt;How do I slice the path to stop at the second to last folder ? for example,&lt;/P&gt;&lt;P&gt;C:\Cases\blah\2020 blah\yoyo\folder1 --&amp;gt; with the current code I get all of this.&lt;/P&gt;&lt;P&gt;C:\Cases\blah\2020 blah\ yoyo --&amp;gt; I needs this populated in the path field&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;rootPath = r'C:\Cases\2020 blah'

shps = []
field = 'Path'
for dirpath, dirnames, files in os.walk(rootPath):
    for x in files:
        if x == "Shapefile.shp":
            shps.append(os.path.join(dirpath, x))

for fc in shps:
    arcpy.AddField_management(fc, field, 'TEXT')
    dirname = os.path.dirname(fc) #just the name of the folder dirname = os.path.basename(os.path.dirname(fc))
    with arcpy.da.UpdateCursor(fc,field) as cur:
        for row in cur:
            row[0]= dirname
            cur.updateRow(row)
        
arcpy.Merge_management(shps, r"C:\Temp\MergedTest.shp")&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 28 Apr 2021 17:31:40 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/meger-certain-shapefiles-within-sub-folders-with/m-p/1052406#M60933</guid>
      <dc:creator>2Quiker</dc:creator>
      <dc:date>2021-04-28T17:31:40Z</dc:date>
    </item>
    <item>
      <title>Re: Meger certain shapefiles within sub folders with path name in attributes</title>
      <link>https://community.esri.com/t5/python-questions/meger-certain-shapefiles-within-sub-folders-with/m-p/1052413#M60934</link>
      <description>&lt;P&gt;When working with file system paths in Python 3.4+, I strongly encourage people to switch to using &lt;A href="https://docs.python.org/3.6/library/pathlib.html" target="_blank"&gt;11.1. pathlib — Object-oriented filesystem paths — Python 3.6.13 documentation&lt;/A&gt;.&lt;/P&gt;&lt;LI-CODE lang="python"&gt;&amp;gt;&amp;gt;&amp;gt; from pathlib import Path
&amp;gt;&amp;gt;&amp;gt;
&amp;gt;&amp;gt;&amp;gt; shp_path = r"C:\Cases\blah\2020 blah\yoyo\folder1\shapefile.shp"
&amp;gt;&amp;gt;&amp;gt; path = Path(shp_path)
&amp;gt;&amp;gt;&amp;gt; print(path.parents[1])
C:\Cases\blah\2020 blah\yoyo
&amp;gt;&amp;gt;&amp;gt;&lt;/LI-CODE&gt;</description>
      <pubDate>Wed, 28 Apr 2021 17:50:38 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/meger-certain-shapefiles-within-sub-folders-with/m-p/1052413#M60934</guid>
      <dc:creator>JoshuaBixby</dc:creator>
      <dc:date>2021-04-28T17:50:38Z</dc:date>
    </item>
    <item>
      <title>Re: Meger certain shapefiles within sub folders with path name in attributes</title>
      <link>https://community.esri.com/t5/python-questions/meger-certain-shapefiles-within-sub-folders-with/m-p/1052503#M60935</link>
      <description>&lt;P&gt;Thanks for the suggestion.&lt;/P&gt;</description>
      <pubDate>Wed, 28 Apr 2021 21:32:31 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/meger-certain-shapefiles-within-sub-folders-with/m-p/1052503#M60935</guid>
      <dc:creator>2Quiker</dc:creator>
      <dc:date>2021-04-28T21:32:31Z</dc:date>
    </item>
  </channel>
</rss>

