Select to view content in your preferred language

search in folders and .gdb and rename featureclass

4389
2
Jump to solution
01-18-2015 07:41 AM
PeterVersteeg
Frequent Contributor

Hallo,

I have a database that are in file folders and in databases (gdb format). It looks a bit like this

Main File folder

                -filefolder “GIS”

                               -database.gdb B

                                               -featureclass J

                                               -featureclass K

                                               -featureclass L 

                               -database.gdb C

                                               -featureclass J

                                               -featureclass K

                                               -featureclass L 

                -Filefolder E

                               -database.gdb F

                                               -featureclass J

                                               -featureclass K

                                               -featureclass L 

                               -database.mgb G

                                               -featureclass J

                                               -featureclass K

                                               -featureclass L 

So the names of the folders and the gdb’s are unique but the featuresclass in each gdb have the same name (not the same factor).

What I want is a script that search for all featureclasses J, K and L and rename them. So far I have:

import arcpy

from arcpy import env

import os

# Set the workspace for the ListFeatureClass function

  1. arcpy.env.workspace = r"D: \GIS\ZONE1\ZONE1_007.gdb"

# Use the ListFeatureClasses function to return a list of all FC.

fclist = arcpy.ListFeatureClasses("*","ALL")

print fclist

# Rename FC

for fc in fclist:

    if fc == "TEKST_XX01P_point":

arcpy.Rename_management("TEKST_XX01P_point ", "P_XX01_house")

    elif fc == " TEKST_XX032_area ":

arcpy.Rename_management("TEKST_XX032_area", "A_XX032_forest")

print fclist

This works for only gdb set in my workspace. Can anyone help me with the code I need to search also the folders.  I was looking at the da.walk function but this is not working for me:

import arcpy

import os

workspace = 😧 \GIS "

feature_classes = []

for dirpath, dirnames, filenames in arcpy.da.Walk(workspace,

                                                  datatype="FeatureClass",

type="Polygon"):

    for filename in filenames:

feature_classes.append(os.path.join(dirpath, filename))

print filename

thank you greeting peter

0 Kudos
1 Solution

Accepted Solutions
XanderBakker
Esri Esteemed Contributor

Hi Peter,

First of all you should really use syntax highlighting, since it makes it easier to see where the error in code are:

Posting Code blocks in the new GeoNet

So you first part of the code, after deleting some redundant code (and spaces) and correcting some indentation would be:

import arcpy

# Set the workspace for the ListFeatureClass function
arcpy.env.workspace = r"D:\GIS\ZONE1\ZONE1_007.gdb"

# Use the ListFeatureClasses function to return a list of all FC.
fclist = arcpy.ListFeatureClasses("*","ALL")

# Rename FC
for fc in fclist:
    if fc == "TEKST_XX01P_point":
        arcpy.Rename_management("TEKST_XX01P_point", "P_XX01_house")
    elif fc == "TEKST_XX032_area":
        arcpy.Rename_management("TEKST_XX032_area", "A_XX032_forest")

The da functionality works if you have access to ArcGIS 10.1 SP1 or higher. If not you may want to read Recursive list feature classes | ArcPy Café. I assume you have access to the da module and what you should read is this post: Inventorying data: a new approach | ArcPy Café  (both by the ArcGIS Team Python).

A simple example is printing the name (full path) of the featureclass:

import os
import arcpy

def inventory_data(workspace, datatypes):
    """
    Generates full path names under a catalog tree for all requested
    datatype(s).

    Parameters:
    workspace: string
        The top-level workspace that will be used.
    datatypes: string | list | tuple
        Keyword(s) representing the desired datatypes. A single
        datatype can be expressed as a string, otherwise use
        a list or tuple. See arcpy.da.Walk documentation
        for a full list.
    """
    for path, path_names, data_names in arcpy.da.Walk(
            workspace, datatype=datatypes):
        for data_name in data_names:
            yield os.path.join(path, data_name)

for feature_class in inventory_data(r"c:\Forum", "FeatureClass"):
    print feature_class

From there you can include the logic for renaming the featureclass.

Kind regards, Xander

View solution in original post

2 Replies
XanderBakker
Esri Esteemed Contributor

Hi Peter,

First of all you should really use syntax highlighting, since it makes it easier to see where the error in code are:

Posting Code blocks in the new GeoNet

So you first part of the code, after deleting some redundant code (and spaces) and correcting some indentation would be:

import arcpy

# Set the workspace for the ListFeatureClass function
arcpy.env.workspace = r"D:\GIS\ZONE1\ZONE1_007.gdb"

# Use the ListFeatureClasses function to return a list of all FC.
fclist = arcpy.ListFeatureClasses("*","ALL")

# Rename FC
for fc in fclist:
    if fc == "TEKST_XX01P_point":
        arcpy.Rename_management("TEKST_XX01P_point", "P_XX01_house")
    elif fc == "TEKST_XX032_area":
        arcpy.Rename_management("TEKST_XX032_area", "A_XX032_forest")

The da functionality works if you have access to ArcGIS 10.1 SP1 or higher. If not you may want to read Recursive list feature classes | ArcPy Café. I assume you have access to the da module and what you should read is this post: Inventorying data: a new approach | ArcPy Café  (both by the ArcGIS Team Python).

A simple example is printing the name (full path) of the featureclass:

import os
import arcpy

def inventory_data(workspace, datatypes):
    """
    Generates full path names under a catalog tree for all requested
    datatype(s).

    Parameters:
    workspace: string
        The top-level workspace that will be used.
    datatypes: string | list | tuple
        Keyword(s) representing the desired datatypes. A single
        datatype can be expressed as a string, otherwise use
        a list or tuple. See arcpy.da.Walk documentation
        for a full list.
    """
    for path, path_names, data_names in arcpy.da.Walk(
            workspace, datatype=datatypes):
        for data_name in data_names:
            yield os.path.join(path, data_name)

for feature_class in inventory_data(r"c:\Forum", "FeatureClass"):
    print feature_class

From there you can include the logic for renaming the featureclass.

Kind regards, Xander

PeterVersteeg
Frequent Contributor

Hi Xander,

First thank you for the syntax highlighting remark. I have the first part off my project working now:

import os  
import arcpy  


workspace = "D:\Documenten\GIS\Zone1\Zone1A"
datatypes = "Polygon"


def inventory_data(workspace, datatypes):  
    """ 
    Generates full path names under a catalog tree for all requested 
    datatype(s). 

    Parameters: 
    workspace: string 
        The top-level workspace that will be used. 
    datatypes: string | list | tuple 
        Keyword(s) representing the desired datatypes. A single 
        datatype can be expressed as a string, otherwise use 
        a list or tuple. See arcpy.da.Walk documentation 
        for a full list. 
    """
    for path, path_names, data_names in arcpy.da.Walk(  
            workspace, datatype=datatypes, type="Polygon"):  
        for data_name in data_names:  
            yield os.path.join(path, data_name)  
  
for feature_class in inventory_data(workspace, "FeatureClass"):  
    print feature_class

i put type in and arcpy.da.Walk also to give me more options. Going to work on the nameing part now.

again thank you.

greetings Peter

0 Kudos