Reputation: 3325
I wish to write a python script which deletes files which weren't accessed in >= x days. I know I can check when the file was created and when it was modified, but can I somehow check when it was last accessed ?
Thanks, Li
Upvotes: 1
Views: 111
Reputation: 596
You can check when the file was last opened using 'stat', Check how much time has passed using 'time', And delete it using 'os':
import os
import stat
import time
fileStats = os.stat(filePath)
last_access = fileStats[stat.ST_ATIME] # in seconds
now = time.time() # in seconds
days = (now - last_access) / (60 * 60 * 24)
# The seconds that have elapsed since the file was last opened are divided by the number of seconds per day
# this give the number of days that have past since the file last open
if x <= days:
# we delete the file
os.remove(filePath)
Upvotes: 2