Select to view content in your preferred language

search in folders and .gdb and rename featureclass part 2

4312
7
Jump to solution
01-18-2015 01:56 PM
PeterVersteeg
Frequent Contributor

Hi,

At the end off part 1 i am able to search folders and Gdb's for featureclasses. for the next part i want to rename a feature in the "feature_class" list.so far i have this:

import os  
import arcpy  


workspace = "D:\GIS\Zone1\Zone1A"
datatypes = "FeatureClass"
arcpy.env.overwriteOutput = True


def inventory_data(workspace, datatypes):  
    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
    if feature_class == "RenameTest":
        arcpy.Rename_management("RenameTes2", "RenameTest2")
print feature_class

can sameone point to me were i am going wrong.

thank you and greetings Peter

0 Kudos
1 Solution

Accepted Solutions
XanderBakker
Esri Esteemed Contributor

It is not necessary to create a double loop, but to provide a sample that does not have that much changed see the script below.

  • I introduced a dictionary "dct" holding the input name and the output name as key, value pair
  • in the second loop the fc is split into the workspace and the featureclass name
  • if the old name is in the dictionary (in the keys) then it is renamed

import arcpy
import os

workspace = "D:\GIS\ZOne1\Zone1A"
feature_classes = []
walk = arcpy.da.Walk(workspace, datatype="FeatureClass", type="Polygon")

dct = {'featureclassName': 'featureclassName_renameTest',
      'featureclassName1': 'featureclassName1_renameTest',
      'another input name': 'corresponding output name'}

for dirpath, dirnames, filenames in walk:
    for filename in filenames:
        feature_classes.append(os.path.join(dirpath, filename))

for fc in feature_classes:
    fc_ws, fc_name = os.path.split(fc)
    if fc_name in dct:
        arcpy.Rename_management(fc, os.path.join(fc_ws, dct[fc_name]))

View solution in original post

7 Replies
Luke_Pinner
MVP Regular Contributor

Can you be more specific about what's "going wrong" - what's your problem, do you get any error messages, etc...?

The 2nd example in the Help is quite useful.

0 Kudos
PeterVersteeg
Frequent Contributor

yes it is but my input for the rename is in a list. the search is ok i got the right list but the rename is not working. i do not have a error

0 Kudos
ModyBuchbinder
Esri Regular Contributor

Hi

I am not sure the function return any list that can be used by the for statement.

Check the examples here: http://resources.arcgis.com/en/help/main/10.2/002z/002z00000011000000.htm

The last few examples are very close to what you need.

Have fun

Mody

0 Kudos
PeterVersteeg
Frequent Contributor

My first script got me the featureclasses with there path, this is not what i want for my rename. now i got this:

import arcpy
import os


workspace = "D:\GIS\ZOne1\Zone1A"
feature_classes = []
walk = arcpy.da.Walk(workspace, datatype="FeatureClass", type="Polygon")


for dirpath, dirnames, filenames in walk:
    for filename in filenames:
        feature_classes.append(os.path.join(dirpath, filename))
        print filename


for fc in filename:
    if fc == "featureclassName":
        arcpy.Rename_management("featureclassName", "featureclassName_renameTest")
    elif fc == "feature




                    elif fc == "featureclassName1":

                            arcpy.Rename_management("featureclassName1", "featureclassName1_renameTest")

(sorry samehow i cannot add more line to the script)

the print on line 13 gives me a list of all featureclasses i want to rename. but my rename is not working i do not get a error

greetings Peter

0 Kudos
PeterVersteeg
Frequent Contributor

looking at this i think i know what wrong but i do not know how to solve it.

the Rename_management in my script do's not know from what .gdb the feature name i use is from.

greetings Peter

0 Kudos
XanderBakker
Esri Esteemed Contributor

It is not necessary to create a double loop, but to provide a sample that does not have that much changed see the script below.

  • I introduced a dictionary "dct" holding the input name and the output name as key, value pair
  • in the second loop the fc is split into the workspace and the featureclass name
  • if the old name is in the dictionary (in the keys) then it is renamed

import arcpy
import os

workspace = "D:\GIS\ZOne1\Zone1A"
feature_classes = []
walk = arcpy.da.Walk(workspace, datatype="FeatureClass", type="Polygon")

dct = {'featureclassName': 'featureclassName_renameTest',
      'featureclassName1': 'featureclassName1_renameTest',
      'another input name': 'corresponding output name'}

for dirpath, dirnames, filenames in walk:
    for filename in filenames:
        feature_classes.append(os.path.join(dirpath, filename))

for fc in feature_classes:
    fc_ws, fc_name = os.path.split(fc)
    if fc_name in dct:
        arcpy.Rename_management(fc, os.path.join(fc_ws, dct[fc_name]))
PeterVersteeg
Frequent Contributor

I am trying to learn arcpy (python) and was looking for a project i can use at work. Perhaps this was a bit to much for now but i am analyzing your script and reading about the parts i do not understand yet.

The script works great and i even understand 80% off the code.

Greetings Peter

0 Kudos