gpt4 book ai didi

python - 按日期排序文件

转载 作者:太空狗 更新时间:2023-10-29 20:34:32 25 4
gpt4 key购买 nike

我在网上找到这个目录检查代码,稍微修改一下,让它打印出添加的文件。有一个浮标时不时地向我发送读数,但有时连接会丢失,它会发送多个文件而不是一个文件。我需要程序按创建日期为我对它们进行排序。有办法做到这一点吗?

import os, time
path_to_watch = 'c://Users//seplema//Documents//arvuti'
before = dict([(f, None) for f in os.listdir (path_to_watch)])
while 1:
after = dict([(f, None) for f in os.listdir (path_to_watch)])
added = [f for f in after if not f in before]
if before == after:
1==1
else:
if len(added)==1:
print added[0]
else:
for i in range (0,len(added)):
print added[i]
time.sleep(10)
before = after

最佳答案

added.sort(key=lambda x: os.stat(os.path.join(path_to_watch, x)).st_mtime)

将按文件的最后修改时间对添加的列表进行排序

在 Windows 上使用 st_ctime 代替 st_mtime 作为创建时间(这并不意味着在其他平台上也是如此)。

关于python - 按日期排序文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6759415/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com