Copy Shapefiles to Enterprise Geodatabase Feature Dataset

4008
10
Jump to solution
07-31-2019 02:26 PM
JaredPilbeam2
MVP Regular Contributor

Is there a way I can copy shapefiles in a directory to an Enterprise Geodatabase Feature Dataset in a stand-alone script? As a monthly update, the shapefiles would replace/update feature classes with the same names in a feature dataset.

I've attempted to copy a shapefile using an SDE connection file with arcpy.CopyFeatures. The script ran but nothing happened.

import arcpy
from arcpy import env
import os

ws = env.workspace = r'path\to\34512.shp'
out_ws = r'path\to\gissql(2).sde'

fcList = arcpy.ListFeatureClasses(ws)

for shapefile in fcList:
    out_featureclass = os.path.join(out_ws, os.path.splitext(shapefile)[0])
    arcpy.CopyFeatures_management(shapefile, out_featureclass)
0 Kudos
10 Replies
JoeBorgione
MVP Emeritus

Look at what Lance Cole‌ provided earlier.  You need to change your workspace each time you use any arcpy.List##() methods. 

A  .shp is not considered a feature class but it's a file instead; use arcpy.ListFiles() instead and use '*.shp' as your wildcard.  Something like this should work, although I haven't tested it:

arcpy.env.workspace = r'C:\path\to\shapefiles'

shapefileList = []
for s in arcpy.ListFiles('*.shp):
  shapefileList.append(s)


fcList = []
arcpy.env.workspace = r'C:\another\path\to\target.gdb'
for f in arcpy.ListFeatureClasses():
    fcList.append(f)

‍‍‍‍‍‍‍‍‍‍‍‍

 your shapefileList will look like this:

['a.shp','b.shp','c.shp']‍‍

While your fcList will look like this:

['a', 'b', 'c']‍‍

You will then compare the contents of the two lists.  You might get away with something like this:

arcpy.env.workspace = r'C:\path\to\shapefiles'

shapefileList = []
for s in arcpy.ListFiles('*.shp):
  shapefileList.append(s)


fcList = []
arcpy.env.workspace = r'C:\another\path\to\target.gdb'
for f in arcpy.ListFeatureClasses():
    fcList.append(f)

#### notice what workspace we are in ^
### you need to get rid of the .shp in each of the elements in sList
### use list comprehension for that:

newList = [s.replace('.shp', '') for s in slist]

for f in fList:
    if f in newList:
      arcpy.DeleteManagement(f)‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

That's what I meant by sans .shp.....

That should just about do it....