ArcPy – Export Features to Geodatabase Created in Same Python Script

arcgis-proarcpyfeature-classfile-geodatabaseworkspace

I am new to learning Python for GIS. I am currently trying to create a script which creates a new geodatabase, then using a list, iterates through the features of an existing geodatabase and exports these features to the new geodatabase I just created earlier in the script. I have ended up receiving the error "TEST_GDB (the new created geodatabase) does not exist or is not supported" multiple times. I researched my problem, and got the closest to a solution at this link Using geodatabase feature dataset created in ArcPy script as output/input location later on in same script? however I am still receiving the same error. It is worth noting that existing geodatabase from which I am exporting the data from is a default geodatabase for an ArcGIS Pro project.

import arcpy
# set initial environments
arcpy.env.workspace = r"C:\Users\My_Name\Documents\GIS\ODU_Home\PYTHON_ODU\Python_Script_Tool_Test\Python_Script_Tool_Test.gdb"
arcpy.env.OverwriteOutput = True

# create a list for all feature classes within old or existing geodatabase
OldData = arcpy.ListFeatureClasses()
print("Features Listed")

#Initialize variables to use in CreateFileGDB tool
out_folder_path = r"C:\Users\My_Name\Documents\GIS\ODU_Home\PYTHON_ODU"
out_name = "Test_GDB"

# create a new geodatabase & set as new workspace
GDB = arcpy.CreateFileGDB_management(out_folder_path, out_name,)
print("New .gdb created")
ws = str(str(GDB) + "\\")
arcpy.env.workspace = ws
print("New Workspace")


exportList = []
print("New list initialized")

#export feature classes from old gdb to new gdb
for Old in OldData:
    arcpy.FeatureClassToGeodatabase_conversion(Old, r"C:\Users\My_Name\Documents\GIS\ODU_Home\PYTHON_ODU\Test_GDB")
    exportlist.append(Old + "_NEW")
print("Existing Feature Classes Exported")

Best Answer

You haven't specified the correct Geodatabase name in this line

arcpy.FeatureClassToGeodatabase_conversion(Old, r"C:\Users\My_Name\Documents\GIS\ODU_Home\PYTHON_ODU\Test_GDB")

The Geodatabase will have a .gdb suffix on the end which you've left off.

arcpy.FeatureClassToGeodatabase_conversion(Old, r"C:\Users\My_Name\Documents\GIS\ODU_Home\PYTHON_ODU\Test_GDB.gdb")

But since you've already got a variable for your new Geodatabase you could just specify that instead

arcpy.FeatureClassToGeodatabase_conversion(Old, GDB)

As an aside, I don't think you need the following line:

ws = str(str(GDB) + "\\")

There is no need to add the trailing slash into the path for arcgis.env.workspace or any other arcpy tool.

GDB = arcpy.CreateFileGDB_management(out_folder_path, out_name,)
print("New .gdb created")
arcpy.env.workspace = GDB
print("New Workspace")

If you need to join a filename to a path use os.path.join() to do it automatically for you.

Related Question