Skip to main content
7 of 8
added 264 characters in body
MjonesGEO
  • 791
  • 1
  • 10
  • 28

ListFeature classes in SDE

I have this code:

def backSDE(ActiveSDE):
    arcpy.env.overwriteOutput = True
    arcpy.env.workspace = ActiveSDE
    fcs = arcpy.ListFeatureClasses()
    print fcs

#Databases
ActiveSDE = r"C:\Users\me\AppData\Roaming\ESRI\Desktop10.1\ArcCatalog\SERVER_SQLEXPRESS.gds\Client_Survey"
backSDE(ActiveSDE)

The database only has feature classes inside of it. No datasets. When I run this I get "None" in the python interpreter. There should be 10 feature classes. What am I doing wrong? I'm running ArcMap 10.1


UPDATED (2)

I now have this code

def listFcsInGDB():
    ''' set your arcpy.env.workspace to a gdb before calling '''
    for fds in arcpy.ListDatasets('','feature') + ['']:
        for fc in arcpy.ListFeatureClasses('','',fds):
            yield os.path.join(arcpy.env.workspace, fds, fc)

#Set workspace environment to geodatabase
arcpy.env.workspace = r"C:\Users\me\AppData\Roaming\ESRI\Desktop10.1\ArcCatalog\PETROPAM_SQLEXPRESS.gds\Client_Surve"

#Get list of feature classes in geodatabase
#FCs = arcpy.ListFeatureClasses()
FCs = listFcsInGDB()

 #Loop through feature classes in list
for FC in FCs:

     #Print the feature class name
     arcpy.AddMessage(FC)

This is the error message it gives me on this line from above: for fds in arcpy.ListDatasets('','feature') + ['']:


UPDATED (3):

Here is my updated code that lists the feature classes from the database connection file.

import arcpy
import os

arcpy.env.workspace = r"C:\Users\me\AppData\Roaming\ESRI\Desktop10.1\ArcCatalog\PFS_Survey.sde"
def listFcsInGDB():
    ''' set your arcpy.env.workspace to a gdb before calling '''
    for fds in arcpy.ListDatasets('','feature') + ['']:
        for fc in arcpy.ListFeatureClasses('','',fds):
            yield os.path.join(arcpy.env.workspace, fds, fc)

#Set workspace environment to geodatabase
arcpy.env.workspace = r"C:\Users\me\AppData\Roaming\ESRI\Desktop10.1\ArcCatalog\PFS_Survey.sde"

#Get list of feature classes in geodatabase
#FCs = arcpy.ListFeatureClasses()
FCs = listFcsInGDB()

 #Loop through feature classes in list
for FC in FCs:

     #Print the feature class name
     arcpy.AddMessage(FC)
     print FC
     arcpy.CopyFeatures_management(FC, "C:/Users/me/Desktop/" + os.path.basename(FC).split('.')[0] + ".shp")

It outputs this:

C:\Users\me\AppData\Roaming\ESRI\Desktop10.1\ArcCatalog\Survey.sde\Client_Survey.DBO.CO_Arapahoe
C:\Users\me\AppData\Roaming\ESRI\Desktop10.1\ArcCatalog\Survey.sde\Client_Survey.DBO.CO_Montezuma
C:\Users\me\AppData\Roaming\ESRI\Desktop10.1\ArcCatalog\Survey.sde\Client_Survey.DBO.CO_Weld
C:\Users\me\AppData\Roaming\ESRI\Desktop10.1\ArcCatalog\Survey.sde\Client_Survey.DBO.Wy_Converse

The problem now is trying to copy it out of the list. I've updated my code to how I want to copy it, but the ListFeature classes has two decimals in the code.

UPDATE 4

I'm starting a new question that is somewhat based off of this one, but different enough to be in a new page. I will link once it's up.

Here's the link to the question about copying several sde connections.
Backup several SDE database connections

MjonesGEO
  • 791
  • 1
  • 10
  • 28