Comparing file last access date

K

kaklis

Hi to all,
what i want is to search a folder, and if the last access date of the
files in that folder is greater than, lets say 7 days, those files
deleted. (Somekind of a file cleaner script)
I had problems with converting

now = today = datetime.date.today()
and
stats = os.stat(file)
lastAccessDate = time.localtime(stats[7])

into matching formats so that
if (now - lastAccessDate) > 7:
delete the file

what i do wrong
Thanks in advance
Antonis
 
M

MRAB

Hi to all,
what i want is to search a folder, and if the last access date of the
files in that folder is greater than, lets say 7 days, those files
deleted. (Somekind of a file cleaner script)
I had problems with converting

now = today = datetime.date.today()
and
stats = os.stat(file)
lastAccessDate = time.localtime(stats[7])

into matching formats so that
if (now - lastAccessDate) > 7:
delete the file

what i do wrong
Thanks in advance
Antonis

I would use:

seven_days = 7 * 24 * 60 * 60 # 7 days in seconds

now = time.time() # in seconds since Epoch

...

last_access = os.path.getatime(path) # in seconds since Epoch
if now - last_access > seven_days:
os.remove(path)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,608
Members
45,241
Latest member
Lisa1997

Latest Threads

Top