【问题标题】:Get last access time of the file?获取文件的最后访问时间?
【发布时间】:2013-01-01 20:35:02
【问题描述】:

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

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

我预计延迟前和延迟后的输出会有所不同。 每次运行代码时,输​​出也相同。

有什么问题吗?

【问题讨论】:

  • 你是否访问了间隔之间的文件?
  • 我认为可能是 unix 中的访问时间在 read(2) 调用时更新,而不是在通过 mmap(2) 读取文件时更新
  • 导入 os.path 然后打印 lastmodified 和 created 见问题 237079
  • @RachelGallen 已修改/已创建!= 已访问

标签: python file inode


【解决方案1】:

也许文件系统是用noatime 选项挂载的

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

检查您的/etc/fstab

更多关于访问时间https://superuser.com/questions/464290/why-is-cat-not-changing-the-access-time

【讨论】:

    【解决方案2】:

    字段 st_atime 会因文件访问而更改,例如,execve(2)、mknod(2)、pipe(2)、utime(2) 和 read(2)(大于零字节)。其他例程,例如 mmap(2),可能会也可能不会更新 st_atime。

    当您运行“python test.py”时,它不会调用 read(2),而是调用 mmap(2)。这就是访问时间没有更新的原因。

    这是“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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-06
      • 1970-01-01
      • 2023-03-06
      • 2014-06-18
      • 2021-10-15
      • 1970-01-01
      相关资源
      最近更新 更多