Search code examples
pythonpython-2.7arcgisarcpy

How do you copy feature classes into a new geodatabase in ArcGIS/Python?


I am trying to copy feature classes from a folder into a new geodatabase, but none of the files will copy into the new geodatabase. Why are they not copying through?

import arcpy
from arcpy import env
env.workspace="C:\\Users\\welchk\\Desktop\\HW6_data\\data\\"
env.overwriteOutput=True

#creating the gdb
arcpy.CreateFileGDB_management("C:\\Users\\welchk\\Desktop\\HW6_data\\data\\", "t_6.gdb")

#creates a list using ListFeatureClasses function
fclist=arcpy.ListFeatureClasses("*", "polygon")

#uses CopyFeatures_management function to copy feature classes in the list to the data folder. 
for fc in fclist:
#Uses the describe function so below you can tell the basename
    fcdesc=arcpy.Describe(fc)
#copies the features using a function that saves the new feature class with the basename.
arcpy.CopyFeatures_management(fc, "C:\\Users\\welchk\\Desktop\\HW6_data\\data\\t_6.gdb"+fcdesc.basename)

Solution

  • "...t_6.gdb" + fcdesc.basename
    

    Your output file path is missing a delimiter.

    If fcdesc.basename = counties, for example, the above code attempts to create ...t_6.gdbcounties (and it'll actually make a shapefile) instead of ...t_6.gdb\\counties (a feature class within the geodatabase).

    Include the closing delimiter and it runs as expected:

    "C:\\[directories]\\t_6.gdb\\" + fcdesc.basename