My django app parses some files uploaded by the user.It is possible that the file uploaded by the user may remain in the server for a long time ,without it being parsed by the app.This can increase in size if a lot of users upload a lot of files.
I need to delete those files not recently parsed by the app -say not accessed for last 24 hours.I tried like this
import os
import time
dirname = MEDIA_ROOT+my_folder
filenames = os.listdir(dirname)
filenames = [os.path.join(dirname,filename) for filename in filenames]
for filename in filenames:
last_access = os.stat(filename).st_atime #secs since epoch
rtime = time.asctime(time.localtime(last_access))
print filename+'----'+rtime
This shows the last accessed times for each file..But I am not sure how I can test if the file access time was within the last 24 hours..Can somebody help me out?
Check out
time.time(). It will allow you to access the current timestamp, in utc time. You can then subtract the current stamp from the file timestamp and see if it’s greater than 24*60*60.http://docs.python.org/library/time.html#time.time
Also, keep in mind that a lot of the time, a Linux filesystem is mounted with noatime, meaning the st_atime variable might not be populated. You should probably use st_mtime, just to be safe, unless you’re 100% sure the filesystem will always be mounted with atimes recorded.
Here’s what should be a working example, I haven’t debugged though.