Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
132 views
in Technique[技术] by (71.8m points)

python - Get last access time of the file?

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...