获取文件的上次访问时间? [英] Get last access time of the file?

查看:57
本文介绍了获取文件的上次访问时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获取上次访问该文件的时间,我尝试了以下代码:

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

但是每次输出都如下:

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.

可能出什么问题了?

推荐答案

字段st_atime可以通过文件访问来更改,例如execve(2),mknod(2),pipe(2),utime(2)和read(2)(大于零字节).其他例程,例如mmap(2),可能会也可能不会更新st_atime.

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.

当您运行"python test.py"时,它不会调用read(2),而是会调用mmap(2).这就是为什么不增加访问时间的原因.

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.

这里是"strace python test.py"的输出

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

这篇关于获取文件的上次访问时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆