Select to view content in your preferred language

Looping thru Databases in order to List Versions

2902
14
08-08-2012 07:14 AM
JasonWinoker
New Contributor
Hi.  I am new to the Python scene, and just started learning about it.  I am trying to figure out how to accomplish what I think is a simple task.  Basically, I want to make a list of versions that are in our sde databases.

This is what I have written so far:

import arcpy
from arcpy import env
env.workspace = "Database Connections"
dbList = ["CIP", "Ems", "MMS", "Prod", "Storm", "Utils", "WCS"]

##sdeDB = "SQL" + str(dbList) + ".sde"
##The result of this line of code is: SQL[CIP, Ems, MMS, Prod, Storm, Utils, WCS].sde & it is not what I want

for list in dbList:
    list.append("SQL"+[dbList]+".sde")
print list

I know that it is primitive, and is probably wrong in so many ways.  Having said that, this is what I am trying to do:

--Look in ???Database Connections??? at the individual databases
--Make a list
--Add ???SQL??? in front of each item in the list
--Add ???.sde??? to the end of each item on the list
     These are the sde database names


For the second part of the script, I???d like to:

--Find the versions in those databases
--See a list of the versions in each of the databases

Here is what I have drafted so far to see the list, but it is only for SQLUtils.sde:

versionList = arcpy.ListVersions(r"Database Connections\SQLUtils.sde")
for version in versionList:
   print version

This script works just fine, but only for SQLUtils.sde, but I want to get it to loop thru sde databases in Database Connections after it appends the ???SQL??? prefix and the ???.sde??? extension to the database name.

I know that I could simply type the "SQL" and ".sde" to the items in the list, but where is the challenge in that?

Any suggestions?  I'd really appreciate your help.

Sincerely,

Jason
Tags (2)
0 Kudos
14 Replies
MathewCoyle
Frequent Contributor
This will work regardless of windows version  or settings.
path = os.path.join(os.getenv("APPDATA"), r"ESRI\Desktop10.0\ArcCatalog")
0 Kudos
BruceBacia
Occasional Contributor
Thanks mzcoyle for the clarification.  Do you know off hand if sde connections are stored by default in the
"C:\\Documents and Settings\\user\\Application Data\\ESRI\\Desktop10.0\\ArcCatalog\\" folder in XP?
0 Kudos
MathewCoyle
Frequent Contributor
Thanks mzcoyle for the clarification.  Do you know off hand if sde connections are stored by default in the
"C:\\Documents and Settings\\user\\Application Data\\ESRI\\Desktop10.0\\ArcCatalog\\" folder in XP?


Yes they should be.
0 Kudos
JasonWinoker
New Contributor
Using the following code, I get this error message:

"TypeError: ListVersions: Could not open SDE Workspace."

import arcpy,os
#path to database connections
path = "C:\\Documents and Settings\\jwinoker\\Application Data\\ESRI\\Desktop10.0\\ArcCatalog\\"
dirs = os.listdir(path)
#list of the full paths of SDE databases in Database Connections
dbs = [(path + dir) for dir in dirs if dir.endswith(".sde")]
for db in dbs:
    dbName = db.replace(path,"")
    print(dbName)
    #print a divider underneath the database Path
    print("-" * len(dbName))
    #for each SDE database, creates a list of its versions
    versions = arcpy.ListVersions(db)
    for version in versions:
        print version
    # create whitespace between each database and list of versions
    print("\n")
0 Kudos
MathewCoyle
Frequent Contributor
Using the following code, I get this error message: 

"TypeError: ListVersions: Could not open SDE Workspace." 

import arcpy,os 
#path to database connections 
path = "C:\\Documents and Settings\\jwinoker\\Application Data\\ESRI\\Desktop10.0\\ArcCatalog\\"  
dirs = os.listdir(path) 
#list of the full paths of SDE databases in Database Connections 
dbs = [(path + dir) for dir in dirs if dir.endswith(".sde")]  
for db in dbs: 
dbName = db.replace(path,"") 
print(dbName) 
#print a divider underneath the database Path 
print("-" * len(dbName)) 
#for each SDE database, creates a list of its versions 
versions = arcpy.ListVersions(db) 
for version in versions: 
print version 
# create whitespace between each database and list of versions 
print("\n")


Have some problems with your paths I would think. Us os.path functions to properly define paths in python. Changing the red bold parts worked for me.

import arcpy
import os
#path to database connections
path = os.path.join(os.getenv("APPDATA"), r"ESRI\Desktop10.0\ArcCatalog")
dirs = os.listdir(path)
#list of the full paths of SDE databases in Database Connections
dbs = [(os.path.join(path, dir)) for dir in dirs if dir.endswith(".sde")]
for db in dbs:
    dbName = db.replace(path,"")
    print(dbName)
    #print a divider underneath the database Path
    print("-" * len(dbName))
    #for each SDE database, creates a list of its versions
    versions = arcpy.ListVersions(db)
    for version in versions:
        print version
    # create whitespace between each database and list of versions
    print("\n")
0 Kudos