简体   繁体   English

Python 列出文件/目录而不更新上次访问时间?

[英]Python listing files/directories without updating last access time?

I wrote a simple code to check specific directory's last access time.我写了一个简单的代码来检查特定目录的最后访问时间。 Below code always prints the first execution time of this script.下面的代码总是打印这个脚本的第一次执行时间。 Another call of this script does not updating it.此脚本的另一个调用不会更新它。 It always shows the same time Wed Dec 16 14:31:38 2020 .它始终显示同一时间Wed Dec 16 14:31:38 2020

How can I get the actual last access time without modifying it from the script call?如何在不从脚本调用中修改它的情况下获得实际的最后访问时间?

import os
import time
import re

result = {}
for root, dirs, files in os.walk('/data/samples/'):
    if re.search(r'.*/[0-9a-f]{40}$', root):
        print(root, time.ctime(os.stat(root)).st_atime)
('/data/samples/a/b/c/4564564564564564564564564564564564564564', 'Wed Dec 16 14:31:38 2020')
('/data/samples/d/e/f/1231231231231231231231231231231231231231', 'Wed Dec 16 14:31:38 2020')
...

Maybe this can help:也许这可以帮助:

https://superuser.com/questions/251263/the-last-access-date-is-not-changed-even-after-reading-the-file-on-windows-7 https://superuser.com/questions/251263/the-last-access-date-is-not-changed-even-after-reading-the-file-on-windows-7

If you actually want the Last Access date updated the way it used to be, simply set the registry value to 0.如果您确实希望按以前的方式更新上次访问日期,只需将注册表值设置为 0。

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

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