How to use arcpy to list Database Connection Files

06-04-2018 02:19 PM
Occasional Contributor II

On my own machine, I know that my Database Connections are in C:\Users\<username>\AppData\Roaming\ESRI\Desktop10.6\ArcCatalog .  I also know that %APPDATA% will take me half-way there.  On another person's machine, the user name will be different, but the version of Desktop might be also.  I need to write, distribute, and run a script in my organization on selected machines.  The script has to find the full path to a database connection that we all have so that I can use it in arcpy.mapping.  I tried arcpy.ListWorkspaces() but that returns None.

What arcpy function can I use to list the full Windows path to database connection files on the current user's machine?

0 Kudos
1 Reply
New Contributor III

This question has been asked for some time now. I had the same doubt today and it seems arcpy still doesn't have such a function, but here it goes an alternative:

import os
import arcpy

def sdeConnections():
    appdata = os.getenv('APPDATA')
    arcgisVersion = arcpy.GetInstallInfo()['Version']
    arcCatalogPath = os.path.join(appdata ,'ESRI',u'Desktop'+arcgisVersion, 'ArcCatalog')
    sdeConnections = []
    for file in os.listdir(arcCatalogPath):
        fileIsSdeConnection = file.lower().endswith(".sde")
        if fileIsSdeConnection:
            sdeConnections.append(os.path.join(arcCatalogPath, file))
    return sdeConnections