List .gdb in a folder location and use the .gdb as a variable?

2788
3
Jump to solution
10-02-2018 01:54 PM
by Anonymous User
Not applicable

List .gdb in a folder location and use the .gdb as a variable?

Tags (2)
0 Kudos
1 Solution

Accepted Solutions
RandyBurton
MVP Alum

Are you asking about a way to walk through a directory and find gdb's?  One way:

import arcpy, os
from arcpy import env

workspace = r"C:\Path\to\explore"  
for dirpath, dirnames, filenames in arcpy.da.Walk(workspace, datatype="Container"):
    for dirname in dirnames:
        if ".gdb" in dirname:
            env.workspace = os.path.join(dirpath, dirname)
            # print env.workspace

            tableList = arcpy.ListFeatureClasses()
            # print tableList
            for table in tableList:
                # print
                print "{}\t{}\t{}".format(dirpath, dirname, table)
‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

View solution in original post

3 Replies
RandyBurton
MVP Alum

Are you asking about a way to walk through a directory and find gdb's?  One way:

import arcpy, os
from arcpy import env

workspace = r"C:\Path\to\explore"  
for dirpath, dirnames, filenames in arcpy.da.Walk(workspace, datatype="Container"):
    for dirname in dirnames:
        if ".gdb" in dirname:
            env.workspace = os.path.join(dirpath, dirname)
            # print env.workspace

            tableList = arcpy.ListFeatureClasses()
            # print tableList
            for table in tableList:
                # print
                print "{}\t{}\t{}".format(dirpath, dirname, table)
‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
by Anonymous User
Not applicable

Randy,

I have a similar issue I need help with if you don't mind. I have a few .xlsx files in a folder location (name example: Bermuda Rd MP 0_26 102017_TestTable.xlsx) and I would like to list the files and then split their names up to use as a query to update data in a ArcOnline feature service. The first part of the file name would be for the RoadName attribute (ex. Bermuda Rd) and the second part of the file name is the Milepost location (ex. 0_26) and the underscore in the file name would need to be replaced with a "." to match the ArcOnline Milepost attribute. How would I list the file names, split the text up and then use those as variables to build my query upon?

Thanks,

Anthony

0 Kudos
DanPatterson_Retired
MVP Emeritus

with arcpy.walk there are a variety of other parameters that might be worth a read since they are specific to Arc* and do 'stuff' that the os equivalent can't do

Walk—Data Access module | ArcGIS Desktop