简体   繁体   English

如何从最近到最近的日期排序

[英]how to order dates from most recent to least recent

So here is this the code I use to insert into the tkinter listbox of downloaded files from the downloads folder to list all.zip files.所以这是我用来插入 tkinter 列表框中的代码,该列表框是从下载文件夹中下载的文件,以列出 all.zip 文件。 How would I be able to make sure it orders the files from most recent to least recent?我如何能够确保它从最近到最近对文件进行排序?

for line in downloads:
            unixtime = os.stat(line).st_mtime
            kws = datetime.fromtimestamp(unixtime)
            if ".zip" in line:
                list.insert(tk.END, str(line), "Date modified: ", kws.date(), "\n")

using pathlib : eg使用路径库:例如

from pathlib import Path

src = Path('/your_downloads_directory')

zipfiles_sorted = sorted(src.glob('*.zip'), key=lambda p: p.lstat().st_mtime)

You can sort the downloads list first based on the modification time in reverse order:您可以先根据修改时间对downloads列表进行倒序排序:

downloads = sorted(((os.stat(line).st_mtime, line) for line in downloads if line.endswith('.zip')), reverse=1)
for unixtime, line in downloads:
    kws = datetime.fromtimestamp(unixtime)
    listbox.insert(tk.END, line, 'Date modified:', kws.date())

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

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