deleting unaccessed files using python

Posted by damon on Stack Overflow See other posts from Stack Overflow or by damon
Published on 2012-07-05T03:01:31Z Indexed on 2012/07/05 3:15 UTC
Read the original article Hit count: 158

Filed under:
|
|

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?

© Stack Overflow or respective owner

Related posts about python

Related posts about file