Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.