简体   繁体   English

在 python 中查找最后一个文件访问

[英]Find last file access in python

I am trying to get the last access file using Python.我正在尝试使用 Python 获取最后一个访问文件。 Even after accessing the files using vi/sublime or any other editor, the access time of the file does not get updated.即使在使用 vi/sublime 或任何其他编辑器访问文件后,文件的访问时间也不会更新。 I have tried to use the function os.stat(full_path).st_atime but of no use.我曾尝试使用 function os.stat(full_path).st_atime但没有用。 It throws out the correct result only if the file is modified.只有当文件被修改时,它才会抛出正确的结果。

Just following up on the link alternative to getatime to find last file access in python只需跟进 getatime 的链接替代方法,即可在 python 中找到最后一个文件访问

You should check this way: 您应该这样检查:

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

I recommend you to check official info at os.stat() documentation : 我建议您在os.stat()文档中查看官方信息:

To check creation&modification dates 查看创建和修改日期

print "last modified: %s" % time.ctime(os.path.getmtime(file))
print "created: %s" % time.ctime(os.path.getctime(file))

OR 要么

(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)

Find last creation file:查找最后一个创建文件:

def findlatestfile(folder):
    list_of_files = glob.glob(folder+'/*') 
    latest_file = max(list_of_files, key=os.path.getctime)
    return latest_file

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM