3

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

3
  • Check the line where you set the workspace in your updated code. You have truncated the y off the end of survey Commented Apr 28, 2014 at 19:54
  • Regarding the latest edit, try changing the [0] on arcpy.CopyFeatures_management(FC, "C:/Users/me/Desktop/" + os.path.basename(FC).split('.')[0] + ".shp") to [-1].
    – blah238
    Commented Apr 28, 2014 at 21:53
  • If you feel like this question was answered please mark it as such.
    – Conor
    Commented Apr 29, 2014 at 14:48

2 Answers 2

4

I have some modified code that lists all feature classes, with or without feature datasets.

Try this:

import arcpy
import os

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 = "Database Connections\\GISDev.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)
3

Try creating an SDE connection file rather than a "Database Server" connection. arcpy doesn't seem to like working directly with those.

1
  • That is true; Database Server connection you establish in the Catalog window is used only for administering purposes. When connecting to the enterprise geodatabase stored in SQL Server Express, you use Database Connections section or create an ArcSDE connection file. Commented Apr 29, 2014 at 6:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.