Select to view content in your preferred language

Recursive conversion from shp to FileGDB through folders and subfolders

985
5
Jump to solution
04-20-2012 09:30 AM
JonathonBruce
Emerging Contributor
I'm trying to convert all shapefiles into feature classes in my directory, including what's inside sub-folders. The code that I've compiled below (from reading previous answers and trial & error on my part) accomplishes this, but it seems that it also begins to iterate through the newly created GDB as well, which is bad. Any advice or insight would be greatly appreciated.


import arcpy from arcpy import env import os  path = 'C:\\Data' env.workspace = path  arcpy.management.CreateFileGDB(env.workspace,"Project","10.0")   try:     def fcs_in_workspace(workspace):         env.workspace = workspace         for fc in arcpy.ListFeatureClasses():             yield fc         for ws in arcpy.ListWorkspaces():             for fc in fcs_in_workspace(os.path.join(workspace, ws)):                 yield fc          OutGDB = env.workspace +'\\Project.gdb'       for fc in fcs_in_workspace(env.workspace):           arcpy.FeatureClassToGeodatabase_conversion(fc,OutGDB)           print(fc +' copied.')                          except Exception as e:     print(e) else:     env.workspace = path     arcpy.CompressFileGeodatabaseData_management(OutGDB)     print(OutGDB + " has been compressed.")         
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
DarrenWiens2
MVP Alum
Is "for fc in fcs_workspace(env.workspace):" supposed to be "for fc in fcs_in_workspace(env.workspace):"? Also, is the indentation above truly correct? It doesn't look like you ever actually call the function "fcs_in_workspace".

In any case, you should somewhere test "if workspace != 'Project.gdb': ", or remove the workspace from your list of workspaces altogether.

View solution in original post

0 Kudos
5 Replies
DarrenWiens2
MVP Alum
Is "for fc in fcs_workspace(env.workspace):" supposed to be "for fc in fcs_in_workspace(env.workspace):"? Also, is the indentation above truly correct? It doesn't look like you ever actually call the function "fcs_in_workspace".

In any case, you should somewhere test "if workspace != 'Project.gdb': ", or remove the workspace from your list of workspaces altogether.
0 Kudos
JonathonBruce
Emerging Contributor
No, the indentation isn't correct and thanks for the catch on fcs_in_workspace. Nerves got the best of me while I was setting up, with it being my first post and all. I will test the != idea and get back to you. Thanks!
0 Kudos
JonathonBruce
Emerging Contributor
Your idea worked! Here is the code as of current:
import arcpy,os
from arcpy import env

path = 'E:\\GDBConversionProject\\2062 Cabin Bluff'
env.workspace = path

arcpy.management.CreateFileGDB(env.workspace,"Project","10.0")


try:
    def fcs_in_workspace(workspace):
        env.workspace = workspace
        for fc in arcpy.ListFeatureClasses():
            yield fc
        for ws in arcpy.ListWorkspaces():
            for fc in fcs_in_workspace(os.path.join(workspace, ws)):
                if ws != path +"\\Project.gdb":
                    yield fc

    OutGDB = env.workspace +'\\Project.gdb'

    for fc in fcs_in_workspace(env.workspace):
        arcpy.FeatureClassToGeodatabase_conversion(fc,OutGDB)
        print(fc +' copied.')
                        
except Exception as e:
    print(e)
else:
    env.workspace = path
    print("Compressing FileGDB.")
    arcpy.CompressFileGeodatabaseData_management(OutGDB)
    print(OutGDB + " has been compressed.")
0 Kudos
MichaelVolz
Esteemed Contributor
Question about the importance of this script:

Is this your companies (organization's) policy to convert shapefiles to file geodatabases?

Or is ESRI dictating this move because shapefiles are going to be deprecated in the near future?
0 Kudos
JonathonBruce
Emerging Contributor
Question about the importance of this script:

Is this your companies (organization's) policy to convert shapefiles to file geodatabases?

Or is ESRI dictating this move because shapefiles are going to be deprecated in the near future?


It was an internal company shift from shapefiles to FGDB's.
0 Kudos