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()