views:

234

answers:

3

Hello,

I have several directories and I want directories over 7 days old to be deleted. I have code already implemented but It doesn't seem to be working. Can anyone see where I am going wrong?

def delete_sandbox():

    for directories in os.listdir(os.getcwd()): 

        if not os.path.isdir(directories) or not os.stat(directories).st_ctime < time.time()-(7*24*3600): 
            continue
        os.chdir(directories)
        drop_sandbox()
        os.chdir(rootDir)
        os.system("sudo rm -rf "+directories)
        print 'Folders older than 7 days old dropped and removed'

Thanks for any help

The folders sandboxes drop but do not delete. I want the program to go into each one of these folders, drop the sandbox, chnage back to the root directory and delete all the old directories. When I do this the folders still exist.

Also this function worked when I had the directories deleted by the string date stored in the folder name. But now that I am trying to get the timestamp it has stopped working.

I have tested the 'rm -rf'+directories and it does not delete old folders. When I try shutil.rmtree I get the error message:

Traceback (most recent call last):
  File "yep.py", line 21, in <module>
    delete_sandbox()
  File "yep.py", line 18, in delete_sandbox
    shutil.rmtree(directories)
  File "/home/build/workspace/downloads/Python-2.6.4/Lib/shutil.py", line 208, in rmtree
    onerror(os.listdir, path, sys.exc_info())
  File "/home/build/workspace/downloads/Python-2.6.4/Lib/shutil.py", line 206, in rmtree
    names = os.listdir(path)
OSError: [Errno 2] No such file or directory: 'Debug'

Is there any other way to delete these folders?

I got it working, I used shutil.rmtree and all seemed to worked. Thanks for any help. The amended code is:

def delete_sandbox():

    for directories in os.listdir(os.getcwd()): 

        if not os.path.isdir(directories) or not os.stat(directories).st_ctime < time.time()-(sbox_age): 
            continue
        os.chdir(directories)
        drop_sandbox()
        os.chdir(rootDir)
        shutil.rmtree(directories)
        print 'Sandboxes older than 7 days old dropped and removed'

delete_sandbox()
+1  A: 

os.listdir returns a list of strings, which are relative paths. When you chdir to rootdir, dependent on what rootDir is, these paths might not be valid anymore.

disown
+1  A: 
  • What does drop_sandbox() do? (The function you gave us is delete_sandbox()) Maybe you meant this to be a recursive function and used the wrong function name
  • Is rootDir a global variable? Perhaps you meant os.chdir("..")
  • What does rootDir contain? os.listdirgives relative paths. If rootDir is the base of your search, the directories you listed may not work. Worse: If they do, you might delete something you still want.

Additionally, the shutil package has a rmtreefunction you might want to look into.

Alex Brault
+2  A: 
import os
import time
import shutil
numdays = 86400*7
now = time.time()
directory=os.path.join("/home","path")
for r,d,f in os.walk(directory):
    for dir in d:
         timestamp = os.path.getmtime(os.path.join(r,dir))
         if now-numdays < timestamp:
             try:
                  print "removing ",os.path.join(r,dir)
                  # shutil.rmtree(os.path.join(r,dir))  #uncomment to use
             except Exception,e:
                  print e
                  pass
             else: 
                  print "some message for success"
ghostdog74
+1 for shutil. Preferred to os.system("rm -rf")
J.J.