【问题标题】:Find last file access in python在python中查找最后一个文件访问
【发布时间】:2014-11-13 07:33:27
【问题描述】:

我正在尝试使用 Python 获取最后一个访问文件。即使使用 vi/sublime 或任何其他编辑器访问文件后,文件的访问时间也不会更新。 我尝试使用函数os.stat(full_path).st_atime,但没有用。只有文件被修改才会抛出正确的结果。

只需关注链接alternative to getatime to find last file access in python

【问题讨论】:

  • atime 需要文件系统支持才能工作。你的好像没有
  • 你在什么操作系统上工作?
  • 我在 Linux (Ubuntu) 上工作。我能够在 Linux 中使用 find 命令找到解决方案:find /home/dir/ -atime -10 -print。但是,我想使用 Python 使用这个命令。有没有办法解决这个问题?

标签: python


【解决方案1】:

你应该这样检查:

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

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

检查创建和修改日期

print "last modified: %s" % time.ctime(os.path.getmtime(file))
print "created: %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)

【讨论】:

  • 感谢 Liarez 回答这个问题,但是当我最近访问文件时,atime 参数没有更新并显示旧时间戳。os.stat() 文档显示 st_atime 只有 1-天分辨率。这是什么意思?
  • 你是如何访问文件的?
  • 使用 vi 或 subl .
  • 并非所有命令都会修改文件的访问权限。如果您想更改文件的访问时间,您可以使用touch -a filename,然后如果您选中atime,它将被更新。我不确定为什么 vi 没有修改 atime
  • 也许这个问题对你有用:Access time does not change after a file is opened
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-09-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多