Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Delete windows temp files
# run by crontab
# removes any files and folders in /tmp/ older than 1 day
# modified from
import os, sys, time, shutil
from subprocess import call
now = time.time()
cutoff = now - (1 * 86400)
files = os.listdir("c:/windows/temp")
for xfile in files:
t = os.stat( "c:/windows/temp/" + xfile )
c = t.st_ctime
# delete file if older than a week
if c < cutoff:
#print xfile
if os.path.isfile( "c:/windows/temp/" + xfile ):
#print 'deleteing file...'
os.remove("c:/windows/temp/" + xfile)
#print 'deleteing directory...'
shutil.rmtree("c:/windows/temp/" + xfile)
except OSError:
# Ignore permission errors, etc, and carry on.
# print "Failed to delete " + xfile
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.