1

I'm trying to make a script, that takes a folder as input, and deletes files older than one week. For some reason, my program does not output expected values.

I used:

os.stat('testFile1.txt').st_mtime
os.stat('testFile1.txt').st_atime

I expected atime to return the time the file was last accessed, and mtime last modification, in seconds.

I get a really high number on both, even though I have just opened a file.

Am I doing something wrong? Should I use another method to get the time?

2
  • 1
    You got "a really high number"? What makes you think the number is wrong? Commented Jan 15, 2018 at 9:17
  • 1
    I think you get two timestamps and you have to take their difference Commented Jan 15, 2018 at 9:22

3 Answers 3

1

The number you are getting is a timestamp in Unix format. It represents the number of seconds since the start of the year 1970 in UTC (that's why it's so big).

In order to convert it to something more usable, you can use datetime.fromtimestamp():

from datetime import datetime

filename = "testFile1.txt"
file_stat = os.stat(filename)
last_modification = datetime.fromtimestamp(file_stat.st_mtime)    
last_access = datetime.fromtimestamp(file_stat.st_atime)

The time you are getting here is not "since the last change". In order to get the amount of time that has passed since a modification or access, you'll need to subtract the modification / access time from the current time:

current_time = datetime.now()
time_since_last_modification = current_time - last_modification
time_since_last_access = current_time - last_access

The code above results in two timedelta objects. In your application, you will need to convert those to days, which is trivial:

days_since_last_modification = time_since_last_modification.days
days_since_last_access = time_since_last_access.days

Whole code

To summarize, this code:

from datetime import datetime

filename = "testFile1.txt"
file_stat = os.stat(filename)
last_modification = datetime.fromtimestamp(file_stat.st_mtime)    
last_access = datetime.fromtimestamp(file_stat.st_atime)

current_time = datetime.now()
time_since_last_modification = current_time - last_modification
time_since_last_access = current_time - last_access

days_since_last_modification = time_since_last_modification.days
days_since_last_access = time_since_last_access.days

msg = "{} was modified {} days ago, with last access {} days ago"
msg = msg.format(filename, days_since_last_modification,
                 days_since_last_access)
print(msg)

Will output something along the lines:

testFile1.txt was modified 4 days ago, last access was 2 days ago
Sign up to request clarification or add additional context in comments.

Comments

0

I believe you are not converting epoch time to datetime From https://docs.python.org/2/library/stat.html

stat.ST_ATIME - Time of last access

time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(os.stat('/tmp/test.txt').st_atime))
>>>'2018-01-15 14:51:23'

stat.ST_MTIME - Time of last modification

time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(os.stat('/tmp/test.txt').st_mtime))
>>>'2018-01-15 14:51:25'

You can even check metadta changes to a file using stat.ST_CTIME

Comments

0

For clarification:

|  st_atime
|      time of last access
|  st_ctime
|      time of last change
|  st_mtime
|      time of last modification

You are getting timestamps. I guess you want it as datetime instead? From that you can use timedelta to find out how old a datetime is.

import os
import datetime


datetime.datetime.fromtimestamp(os.stat("test").st_mtime)
datetime.datetime.now() - datetime.datetime.fromtimestamp(os.stat("test").st_mtime)

Gives output:

datetime.datetime(2018, 1, 11, 8, 23, 23, 913330)
datetime.timedelta(4, 7252, 17055)

From input data:

drobban@xps:~/Desktop$ ls -lrt test
-rw-rw-r-- 1 drobban drobban 0 jan 11 08:23 test

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.