Freebie: Python Chron to Delete Files Older than X

Several of my ASP.NET web applications on Cloud create large numbers of log files on a fairly regular basis and it is necessary for me to prune those logs from time to time. Because ASP.NET in medium trust can’t talk to the /logs folder which automatically purges itself every 6 days, I built a Python script which will automatically delete all files older than a specified time interval within a given directory.

If you need this functionality just save the script, update the path accordingly and set up a Chron job to call it. The code below is set to delete files older than 7 days (7 * 86400), customize as needed. Enjoy!

 # Purges all ELMAH log files older than 7 days and all tempfiles older than 3 days.

 import os, time
 def purgeDir(dir, age):
 	print "Scanning:", dir
 	for f in os.listdir(dir):
 		now = time.time()
 		filepath = os.path.join(dir, f)
 		modified = os.stat(filepath).st_mtime
 		if modified < now - age: 
 			if os.path.isfile(filepath):
 				print 'Deleted: %s (%s)' % (f, modified)

 # 1 Day	= 86400 seconds
 purgeDir("/system/path/to/folder", (7 * 86400))




I am a new python. Can you help me with this please?

I setup tool and it’s ok for me. But this source is difficult.

I want to use simple url to send request to server API and get response.


But I dont know how to set API KEY and user for this link

Can you help me please?



You can find your API key in the control panel under “Your Account” -> “API Access”.

Your first request to cloud files needs to be to authenticate with the Rackspace Cloud auth system. You will then be given a token that you will use for each subsequent request until the token expires. The language bindings (Python in your case) abstract this process and offer a simpler way to access Cloud Files.

You can find this entire process, and more, described in our developer docs.