Select to view content in your preferred language

Add parent directory name to output feature class with os walk

1104
4
Jump to solution
08-22-2022 08:32 PM
KyleSmiley
Emerging Contributor

Hello,

 

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.

 

Any help appreciated!

Here is the code:

 

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)

 

 

Tags (2)
1 Solution

Accepted Solutions
JohannesLindner
MVP Frequent Contributor
# 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) 
        #...

Have a great day!
Johannes

View solution in original post

4 Replies
JohannesLindner
MVP Frequent Contributor

With the os.path module:

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)

 

With the newer pathlib module:

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

Have a great day!
Johannes
0 Kudos
KyleSmiley
Emerging Contributor

Hello,

 

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? 

maybe something like:

for file in filenames:
       for “Pacels” in filenames:

 

or 

If filenames is “Parcels”

 

thanks in advance

 and so on….

 

0 Kudos
JohannesLindner
MVP Frequent Contributor
# 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) 
        #...

Have a great day!
Johannes
KyleSmiley
Emerging Contributor

Good morning,

 

thanks a ton! It worked!

 

Kyle

0 Kudos