9

I want to get when the file accessed last time, I tried 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 is same as follows :

1358489344.72
1358489344.72

I was expecting a difference in output before delay and after delay. also output is same wen I run the code every time.

what could be wrong?

4
  • Do you access the file between the interval?
    – freestyler
    Jan 18, 2013 at 6:49
  • 2
    I think it may be that access time in unix updated on read(2) calls, not updated when files are read via mmap(2)
    – wim
    Jan 18, 2013 at 6:57
  • Import os.path and then print lastmodified and created see question 237079 Jan 18, 2013 at 6:58
  • 2
    @RachelGallen modified/created != accessed
    – Basic
    Nov 7, 2013 at 9:18

2 Answers 2

7

The field st_atime is changed by file accesses, for example, by execve(2), mknod(2), pipe(2), utime(2) and read(2) (of more than zero bytes). Other routines, like mmap(2), may or may not update st_atime.

While you run "python test.py", it won't call read(2), instead it would call mmap(2). That's why the access time didn't be udpated.

Here is 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
0
1

Maybe the filesystem is mounted with noatime option

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

check your /etc/fstab

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

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.