Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
File 如何在Python3中按mtime对给定的文件列表进行排序?_File_Sorting_Python 3.x_Filemtime - Fatal编程技术网

File 如何在Python3中按mtime对给定的文件列表进行排序?

File 如何在Python3中按mtime对给定的文件列表进行排序?,file,sorting,python-3.x,filemtime,File,Sorting,Python 3.x,Filemtime,我正在尝试执行一个函数,该函数接收文件列表(绝对路径),并返回按文件的mtime排序的列表。请注意,参数是文件列表,而不是目录路径 有人能帮我吗?先谢谢你 编辑 import os lista = [] path = 'my/custom/path/' for dirname, dirnames, filenames in os.walk(path): for file in filenames: filepath = os.path.realpath(os.path.

我正在尝试执行一个函数,该函数接收文件列表(绝对路径),并返回按文件的mtime排序的列表。请注意,参数是文件列表,而不是目录路径

有人能帮我吗?先谢谢你

编辑

import os

lista = []
path = 'my/custom/path/'
for dirname, dirnames, filenames in os.walk(path):
    for file in filenames:
        filepath = os.path.realpath(os.path.join(dirname, file))
        lista.append(filepath)
这样我就得到了列表(路径和子路径中的每个文件),现在我需要按mtime对它进行排序

您只需要:

sorted_list = sorted(lista, key=lambda f: os.stat(f).st_mtime)

这将为您提供按mtime排序的文件列表。

到目前为止您尝试了什么?我只能通过路径而不是文件列表来管理按mtime排序(具有排序功能)您可以提供您尝试过的内容的列表,以便我们可以开始帮助您解决实际问题吗?我尝试了许多不同的方法,但我失败了。这就是我需要的,非常感谢!