Skip to content

Instantly share code, notes, and snippets.

@leighghunt
Last active February 2, 2016 21:39
Show Gist options
  • Save leighghunt/7dd1c5d460ae813341a3 to your computer and use it in GitHub Desktop.
Save leighghunt/7dd1c5d460ae813341a3 to your computer and use it in GitHub Desktop.
Delete windows temp files
#!/usr/bin/python
# run by crontab
# removes any files and folders in /tmp/ older than 1 day
# modified from http://pp19dd.com/2013/10/python-script-to-remove-tmp-files-older-than-7-days/
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
try:
if os.path.isfile( "c:/windows/temp/" + xfile ):
#print 'deleteing file...'
os.remove("c:/windows/temp/" + xfile)
else:
#print 'deleteing directory...'
shutil.rmtree("c:/windows/temp/" + xfile)
except OSError:
# Ignore permission errors, etc, and carry on.
# print "Failed to delete " + xfile
pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment