【问题标题】:alternative to getatime to find last file access in python替代 getatime 在 python 中查找最后一个文件访问
【发布时间】:2014-11-13 15:53:56
【问题描述】:

我正在尝试查看某些已安装的应用程序在系统上使用了多少次。

我在我的python脚本中使用过

fileaccesstime=os.path.getatime(os.sep.join([dirpath, filename]))

某些应用程序似乎在访问时也不更新。比如我用了python,看到atime没有变化。

Get last access time of the file? 看来,atime 可能不会更新,因为它会调用 mmap。

这是否意味着我永远找不到上次访问时间。我不确定如何继续找出某些应用程序的最后访问权限,例如。蟒蛇

【问题讨论】:

  • 应用程序不会及时更新,操作系统会(或不会)。例如,使用noatime 选项挂载的 Unix 文件系统永远不会更新。这通常是因为更新 atime 实际上将读取操作转换为写入操作。因此,除非您有机会更改挂载选项,否则 atime 通常不可靠。

标签: python


【解决方案1】:

你应该这样检查:

import os, time
print time.ctime(os.stat('my_path/test.txt').st_atime)

(mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime) = os.stat(file)
print "Last Access: %s" % time.ctime(atime)

我建议您查看os.stat() documentation的官方信息:

检查创建和修改日期

print "Modification Date: %s" % time.ctime(os.path.getmtime(file))
print "Creation Date: %s" % time.ctime(os.path.getctime(file))

(mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime) = os.stat(file)
print "Modification Date: %s" % time.ctime(mtime)
print "Creation Date: %s" % time.ctime(ctime)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-23
    • 2015-01-28
    • 1970-01-01
    相关资源
    最近更新 更多