Python script to Compact geodatabases in multiple directories?

4682
2
Jump to solution
01-07-2015 12:00 PM
JasonDowdy
New Contributor II

I am trying to write a python script to walk through all folders in my workspace and find and compact all geodatabases in my workspace. I am using the 10.2.2 release.

import acrpy, os 
from arcpy import env  

arcpy.env.workspace "C:"  

for (path, dirs, files) in os.walk(arcpy.env.workspace) 
    for file in files if ".gdb" in file:
        workspaces = arcpy.ListWorkspaces("*", "FileGDB") 
        for workspace in workspaces: 
            arcpy.Compact_management(workspace)
0 Kudos
1 Solution

Accepted Solutions
JakeSkinner
Esri Esteemed Contributor

Hi Jason,

You can use the arcpy.da.Walk function to walk through the directories and then compact each geodatabase.  Here is an example:

import arcpy, os
workspace = r"C:\temp\python"
for dirpath, dirnames, filenames in arcpy.da.Walk(workspace, datatype="Container"):
    for dirname in dirnames:
        if ".gdb" in dirname:
            arcpy.Compact_management(os.path.join(dirpath, dirname))
            print "Successfully compacted " + dirname

View solution in original post

2 Replies
JakeSkinner
Esri Esteemed Contributor

Hi Jason,

You can use the arcpy.da.Walk function to walk through the directories and then compact each geodatabase.  Here is an example:

import arcpy, os
workspace = r"C:\temp\python"
for dirpath, dirnames, filenames in arcpy.da.Walk(workspace, datatype="Container"):
    for dirname in dirnames:
        if ".gdb" in dirname:
            arcpy.Compact_management(os.path.join(dirpath, dirname))
            print "Successfully compacted " + dirname
JasonDowdy
New Contributor II

Works great, I did finally get the other method working but this is much better than what I was trying to do.  Much appreciated!

0 Kudos