1

In a folder I have a group of file geodatabases that have unique names from one another. Within each geodatabase is a polyline feature class that is not uniquely named - they are all called 'Elev_Contour'. I want to use Python to iterate through the geodatabases and append the geodatabase name onto the end of the feature class names to make them unique. After that is complete, I want to copy the feature classes into one new geodatabase.

I've used da.walk to build a list of all the feature classes, but I am struggling with how to access just the geodatabase name when iteratively renaming the feature classes.

import os
import arcpy
from arcpy import env
env.workspace = r"b:\contours"
fclist = []
for dirpath, dirnames, filenames in arcpy.da.Walk(env.workspace,datatype="FeatureClass"):
 for filename in filenames:
 fclist.append(os.path.join(dirpath, filename))
for fc in fclist:
 print fc
b:\contours\Elev_321168_Albany_E_1X1.gdb\Elevation\Elev_Contour
b:\contours\Elev_321169_Albany_W_1X1.gdb\Elevation\Elev_Contour
b:\contours\Elev_321240_Binghamton_E_1X1.gdb\Elevation\Elev_Contour
b:\contours\Elev_321241_Binghamton_W_1X1.gdb\Elevation\Elev_Contour
b:\contours\Elev_321259_Boston_W_1X1.gdb\Elevation\Elev_Contour
b:\contours\Elev_321278_Buffalo_E_1X1.gdb\Elevation\Elev_Contour
b:\contours\Elev_321279_Buffalo_W_1X1.gdb\Elevation\Elev_Contour
b:\contours\Elev_321420_Elmira_E_1X1.gdb\Elevation\Elev_Contour
b:\contours\Elev_321421_Elmira_W_1X1.gdb\Elevation\Elev_Contour
b:\contours\Elev_321482_Glens_Falls_E_1X1.gdb\Elevation\Elev_Contour
b:\contours\Elev_321483_Glens_Falls_W_1X1.gdb\Elevation\Elev_Contour
b:\contours\Elev_321516_Harrisburg_E_1X1.gdb\Elevation\Elev_Contour
b:\contours\Elev_321517_Harrisburg_W_1X1.gdb\Elevation\Elev_Contour
b:\contours\Elev_321600_Kingston_E_1X1.gdb\Elevation\Elev_Contour
PolyGeo
65.5k29 gold badges115 silver badges349 bronze badges
asked Dec 1, 2016 at 17:18
1
  • You could use string slicing: fc[11:-14] Commented Dec 1, 2016 at 17:49

2 Answers 2

5

Rather than using string indexing as suggested by @Bryce Frank, I would suggest using a potentially more stable approach and using the native python string function of .split in combination with the python os module value of os.sep and os.path.splitext to remove the .gdb file extension.

Because the position, eg index of your gdb name portion of your split path, remains the same you can use this index value to retrieve the gdb name like this:

gdb_name_index = 2
for fc in fclist:
 gdb_name = os.path.splitext(fc.split(os.sep)[gdb_name_index])[0]
 updated_fc_name = fc + gdb_name 

updated_fc_name can then be used in an operation to copy the feature class to the new geodatabase.

You could further generalize the process and create a function that gets the index of the file geodatabase portion of the file path. Like this:

def get_gdb_path_component_index(in_path):
 gdb_index = None
 in_path_components = in_path.split(os.sep) 
 for component in in_path_components:
 if '.gdb' in component:
 gdb_index = in_path_components.index(component)
 return gdb_index

Because a geodatabase cannot be inside a geodatabase this should be safe. However you would have to check the return variable gdb_index for the value of None before using it.

Putting this all together you would end up with a more generalized workflow like this:

for fc in fclist:
 gdb_name_index = get_gdb_path_component_index(fc)
 if gdb_name_index != None:
 gdb_name = os.path.splitext(fc.split(os.sep)[gdb_name_index])[0]
 updated_fc_name = fc + gdb_name
answered Dec 1, 2016 at 20:36
1
  • 1
    os.path.dirname(os.path.dirname(x)) might be simpler to understand at first glance. Commented Dec 1, 2016 at 21:32
0

You could trim each of these strings if you just want to return a string of the geodatabase name. This works because each is surrounded by a constant number of characters.

This is an example of how to do one, I'll let you incorporate it into your for loop.

str1 = r"b:\contours\Elev_321168_Albany_E_1X1.gdb\Elevation\Elev_Contour"
str1 = str1[12:-23]
print str1
Elev_321168_Albany_E_1X1.gdb
artwork21
35.2k8 gold badges69 silver badges135 bronze badges
answered Dec 1, 2016 at 17:53
1
  • 1
    While this might work in practice, string slicing on paths is not a good idea. Commented Dec 1, 2016 at 21:32

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.