4

I am using ArcGIS Desktop 10.0 and Python 2.6.6 and am trying to make an update script to copy the geodatabase to a backup folder with the date as part of the name, got that to work.

Then export features from my editing geodatabase to the one on our server, got that working.

But now I want to, at the end of my script, to go back to the backup folder and delete any geodatabases there that are older than 1 week to keep it clean.

Tried a number of ways but they didn't work, any suggestions?

import re
import os
import datetime
import time
import sys

from subprocess import call

now = time.time()
cutoff = now - (7 * 86400)

files = os.listdir("C:\GIS Info\Test\Backup")
for xfile in files:
        if os.path.isfile( "C:\GIS Info\Test\Backup" + xfile ):
                t = os.stat( "C:\GIS Info\Test\Backup" + xfile )
                c = t.st_ctime

                # delete file if older than a week
                if c < cutoff:
                        os.remove("C:\GIS Info\Test\Backup" + xfile)

This is the code I was trying to use that got me the furthest.

  • Are you wanting the update script to be in python, or would shell command work as well? – evv_gis Nov 5 '14 at 20:02
  • 2
    What do you mean by "it didn't work"? Were you unable to delete or unable to filter to the correct geodatabases? – Beck Nov 5 '14 at 20:10
  • for delete: arcpy.Delete_management(full_path), for last access time see stackoverflow.com/questions/16464228/… – Michael Stimson Nov 5 '14 at 21:31
  • Can you include a code snippet of how you are trying to "go back to the backup folder and delete any geodatabases there that are older than 1 week", please? No need to show us all the ways that you have tried, just the one that you thought should have been the most likely to work. Also include any error messages thrown by running that code snippet. Also, what type(s) of geodatabases are you trying to delete? – PolyGeo Nov 5 '14 at 23:01
  • 2
    how are you determining that GDBs are older than one week? Are you using the file system time, as @MichaelMiles-Stimson is suggesting, or using the GDB name itself (you mention that the date is part of the file name)? – Stephen Lead Nov 5 '14 at 23:08
2

Since FGDB's are treated as folders, this script looks for folders in a directory older that seven days, then uses the Delete management tool to remove the old folders.

import os
import time
import arcpy

dirPath = r"C:\test_data"
cutoff =  7 * 86400
present = time.time()
directories = []
for root, dirs, files in os.walk(dirPath, topdown=False):
    for name in dirs:
        subDirPath = os.path.join(root, name)
        if (present - os.path.getmtime(subDirPath)) > cutoff:
            directories.append(subDirPath)

for gdb in directories:
    arcpy.Delete_management(gdb)

It doesn't work only on GDB's, it deletes all sub directories older that seven days. But you could tweak it to only delete GDB's and leave other directories in place.

  • That's got it, thanks. Don't think there will be an issue with other directories Backup should only hold the backedup databases. – Tyler Veinot Nov 6 '14 at 17:19

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.