1

This question stems from my previous question on how to list databases based on SDE. Now I want to copy them and have it written as a function so I can list out each database connection below it so it's easy to manage and update. Here's the code that lists the databases. I added the CopyFeature_Management, which may be different that what I want.

import arcpy
import os

arcpy.env.workspace = r"C:\Users\me\AppData\Roaming\ESRI\Desktop10.1\ArcCatalog\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\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
    print FC
    print os.path.basename(FC)[0]
    print os.path.splitext(FC)
    output = "C:/Users/me/Desktop/"
    arcpy.CopyFeatures_management(FC, output + os.path.basename(FC).split('.')[-1] + ".shp")

    #os.path.basename(MXDPath).split('.')[0] + ".pdf"   .split('.')[0] + ".shp"

Any advice how to get the workspace and output as different variables listed below the function? I want them outputted to different folders based off of their database name. It works right now, but only for this one database connection.


UPDATE 2

So now I have this working where it creates a GDB based on the SDE connection name. It will copy the contents and put them in the GDB. The only problem I get is it always leaves one FC out of the file geodatabase. if there are 19 features in survey it only copies 18 to the file GDB

import arcpy
import os

#Set workspace environment to the GDB locations.
arcpy.env.workspace = "C:/Users/me/AppData/Roaming/ESRI/Desktop10.1/ArcCatalog/"

#get every SDE connection we have. We can narrow it down by replacing the wildcard "*" with something else.
workspaces = arcpy.ListWorkspaces("*", "SDE")

#workaround to get feature classes within and outside feature datasets
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)


for workspace in workspaces:
    arcpy.env.overwriteOutput = True
    #list the current workspace we are looping through
    arcpy.AddMessage(workspace)
    #set the current environment to the SDE or GDB instance we want to loop through
    arcpy.env.workspace = workspace
    FCs = listFcsInGDB()
     #Loop through feature classes in SDE or GDB
    for GDB in FCs:
        output = arcpy.CreateFileGDB_management("C:/Users/me/Desktop/", os.path.basename(GDB)[:14] + time.strftime('_%y_%m_%d_%H%M'))

        for FC in FCs:
            arcpy.env.overwriteOutput = True
            ##output = arcpy.CreateFileGDB_management("C:/Users/me/Desktop/", os.path.basename(FC)[:14])
            #output = "C:/Users/me/Desktop/" + os.path.basename(FC)[:14]+"/"
            #os.makedirs(output)

            arcpy.FeatureClassToGeodatabase_conversion(FC, output)
            print str(FC) + " Moved to " + str(output)

Any ideas why its moving them all but one from each? One of the GDBs has only one fc in it. It creates the GDB, but the database is actually empty.


UPDATE 3

import arcpy
import os
timestr = time.strftime("%Y_%m_%d_%H:%M")

#Set workspace environment to the GDB locations.
arcpy.env.workspace = "C:/Users/me/AppData/Roaming/ESRI/Desktop10.1/ArcCatalog/"

#get every SDE connection we have. We can narrow it down by replacing the wildcard "*" with something else.
workspaces = arcpy.ListWorkspaces("*", "SDE")

#workaround to get feature classes within and outside feature datasets
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)


for workspace in workspaces:
    #arcpy.env.overwriteOutput = True
    #list the current workspace we are looping through
    arcpy.AddMessage(workspace)
    #set the current environment to the SDE or GDB instance we want to loop through
    arcpy.env.workspace = workspace
    FCs = listFcsInGDB()
     #Loop through feature classes in SDE or GDB

    output = arcpy.CreateFileGDB_management("C:/Users/me/Desktop/", os.path.basename(workspace)[:14] + time.strftime('_%y_%m_%d_%H%M'))

    for FC in FCs:
        arcpy.env.overwriteOutput = True
        ##output = arcpy.CreateFileGDB_management("C:/Users/me/Desktop/", os.path.basename(FC)[:14])
        #output = "C:/Users/me/Desktop/" + os.path.basename(FC)[:14]+"/"
        #os.makedirs(output)

        arcpy.FeatureClassToGeodatabase_conversion(FC, output)
        print str(FC) + " Backed up to: " + str(output)
  • You shouldn't have to loop through the FCs construct twice. Try removing your outer for loop ("for GDB in FCs") and use "workspace" instead of "GDB" in os.path.basename – Conor Apr 29 '14 at 19:20
  • Changed the code, but the end result is the same. It's still missing a feature class from each SDE connection. – MjonesGEO Apr 29 '14 at 19:38
  • You may want to check the naming conventions of your files & simplify. Your code works as expected on my end with some sample files. – Conor Apr 29 '14 at 20:20
1

Looking at your code and your description, it appears that you want to loop through all your SDE connections and create a shapefile copy of all the feature classes in each to a separate location. Here is some code that lets you either loop through every SDE connection or just the ones you specify.

Try this:

import arcpy
import os

#Set workspace environment to the GDB locations. 
arcpy.env.workspace = "C:/Users/me/AppData/Roaming/ESRI/Desktop10.1/ArcCatalog/"

#get every SDE connection we have. We can narrow it down by replacing the wildcard "*" with something else.
workspaces = arcpy.ListWorkspaces("*", "SDE")

#workaround to get feature classes within and outside feature datasets
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)


for workspace in workspaces:

    #list the current workspace we are looping through
    arcpy.AddMessage(workspace)
    #set the current environment to the SDE or GDB instance we want to loop through
    arcpy.env.workspace = workspace
    #get all feature classes in the SDE or GDB instance
    FCs = listFcsInGDB()
     #Loop through all the feature classes
    for FC in FCs:

        # do all your feature class stuff here

EDIT: I'll also add that I'm not sure what your use cases are, but there are much better and cleaner ways to do backups of your SDE if that is what you are trying to do. You may be interested in this ESRI guide on backups, or replication.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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