Python script not looping through SDE databases

Jump to solution
09-16-2019 06:22 AM
New Contributor II


I am trying to loop through a list in python and the script is only processing the first database and not the remainder of the list. Where is the script wrong? 

1 # import packages
2 import os, arcpy, time, smtplib
3 # setup workspace and SDE databases in folder
4 for dirpath, dirnames, filenames in os.walk(r'C:\Users\XXX\AppData\Roaming\Esri\Desktop10.7\ArcCatalog\SDE'):
5    for file in dirpath:
6        print filenames
7        string = 'C:/Users/XXX/AppData/Roaming/Esri/Desktop10.7/ArcCatalog/SDE/'
8        new_list = [string + x for x in filenames]
9        print new_list
10       for database in new_list:
11          # Block new connections to the database.
12          print('The database is no longer accepting connections{}'.format(database))
13          arcpy.AcceptConnections(database, False)
14          # Wait 5 minutes
15          time.sleep(5)
16          # Disconnect all users from the database.
17          print("Disconnecting all users")
18          arcpy.DisconnectUser(database, "ALL")
19          # Run the compress tool.
20          print("Running compress")
21          arcpy.Compress_management(database)
22          # Allow the database to begin accepting connections again
23          print("Allow users to connect to the database again")
24          arcpy.AcceptConnections(database, True)
25          # Rebuild indexes in database(s)
26          print("Rebuilding indexes on the system tables")
27          arcpy.RebuildIndexes_management(database, "SYSTEM")
28          # Updating statistics in database(s)
29          print("Updating statistics on the system tables")
30          arcpy.AnalyzeDatasets_management(database, "SYSTEM")
31          print ("Finished")
32          break
33      break
34  break
0 Kudos
1 Solution

Accepted Solutions
MVP Esteemed Contributor

Line #32, your break statement is terminating the loop after the first pass.

View solution in original post

3 Replies
MVP Esteemed Contributor

Line #32, your break statement is terminating the loop after the first pass.

New Contributor II

Thanks Joshua

0 Kudos
MVP Esteemed Contributor

I suggest skipping the dirpath approach, and keep it in arcpy by either first setting the workspace env or going directly to your sde connections. Here is an example of using the workspace env:

arcpy.env.workspace = r'C\users\XXX\AppData\Roaming\Esri\Desktop10.7\ArcCatalog\SDE'
gdbList = arcpy.ListWorkspaces('*','SDE')

for gdb in gdbList:
    """execute your analyze compress
       routines; I suggest wrapping
       them in a try/except block"""


I assume that user XXX has admin rights to the Egdbs....

edited to ad:  Looks like Joshua caught the actual error....

That should just about do it....
0 Kudos