基于时间的目录列表 [英] Directory Listing based on time

查看:83
本文介绍了基于时间的目录列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何根据时间戳记列出目录中的文件?

How to list the files in a directory based on timestamp?

 os.listdir() 

以任意顺序列出。

是否有基于时间戳列表的内置函数?或者通过任何订单?

Is there a build-in function to list based on timestamp? or by any order?

推荐答案

您可以调用 stat() ,并按其中一个时间戳排序,也许通过使用返回的键功能一个文件的时间戳。

You could call stat() on each of the files and sort by one of the timestamps, perhaps by using a key function that returns a file's timestamp.

import os

def sorted_ls(path):
    mtime = lambda f: os.stat(os.path.join(path, f)).st_mtime
    return list(sorted(os.listdir(path), key=mtime))

print(sorted_ls('documents'))

这篇关于基于时间的目录列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆