Get the latest file access time?

I want to get when the file was last accessed, I tried the following code:

import os, time os.system("python test.py") print os.stat('test.py').st_atime time.sleep(60) os.system("python test.py") print os.stat('test.py').st_atime 

But each time the output will be the same as below:

 1358489344.72 1358489344.72 

I expected the difference in output before the delay and after the delay. also the output is the same as I run the code every time.

what could be wrong?

+7
source share
2 answers

The st_atime field is changed by accessing files, for example, using execve (2), mknod (2), pipe (2), utime (2), and read (2) (greater than zero). Other procedures, such as mmap (2), may or may not update st_atime.

As long as you run "python test.py", it will not call read (2), instead it will call mmap (2). Therefore, the access time has not been verified.

Here is the output of "strace python test.py"

 open("test.py", O_RDONLY) = 3 fstat(3, {st_mode=S_IFREG|0644, st_size=36, ...}) = 0 mmap(NULL, 65536, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x2ad626cdd000 
+6
source

Perhaps the file system is mounted with the noatime option

 noatime Do not update inode access times on this filesystem (eg, for faster access on the news spool to speed up news servers). 

check /etc/fstab

More about access time https://superuser.com/questions/464290/why-is-cat-not-changing-the-access-time

+1
source

All Articles