如何在 python 中获得按创建日期排序的目录列表?
获取目录中所有文件的列表(按日期排序)的最佳方法是什么[创建| 修改],在 Windows 机器上使用 python?
What is the best way to get a list of all files in a directory, sorted by date [created | modified], using python, on a windows machine?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(19)
我过去曾使用 Python 脚本来确定目录中最后更新的文件:
这应该可以根据文件 mtime 执行您要查找的操作。
编辑:请注意,如果需要,您还可以使用 os.listdir() 代替 glob.glob() - 我在原始代码中使用 glob 的原因是我只想使用 glob搜索具有一组特定文件扩展名的文件,glob() 更适合。 要使用 listdir,它看起来像这样:
I've done this in the past for a Python script to determine the last updated files in a directory:
That should do what you're looking for based on file mtime.
EDIT: Note that you can also use os.listdir() in place of glob.glob() if desired - the reason I used glob in my original code was that I was wanting to use glob to only search for files with a particular set of file extensions, which glob() was better suited to. To use listdir here's what it would look like:
更新:在 Python 3 中按修改日期对
dirpath
的条目进行排序:(put @Pygirl 的回答在这里以获得更好的可见性)
如果您已经有一个文件名
文件
列表,那么在 Windows 上按创建时间对其进行排序(确保该列表包含绝对路径):您可以获得的文件列表,例如,使用
glob
,如 中所示@Jay 的回答。旧答案
这是
@Greg Hewgill
的回答。 是最符合题目要求的。 它区分了创建日期和修改日期(至少在 Windows 上)。例子:
Update: to sort
dirpath
's entries by modification date in Python 3:(put @Pygirl's answer here for greater visibility)
If you already have a list of filenames
files
, then to sort it inplace by creation time on Windows (make sure that list contains absolute path):The list of files you could get, for example, using
glob
as shown in @Jay's answer.old answer
Here's a more verbose version of
@Greg Hewgill
's answer. It is the most conforming to the question requirements. It makes a distinction between creation and modification dates (at least on Windows).Example:
有一个 os.path.getmtime 函数可以给出自纪元以来的秒数
并且应该比 os.stat 更快。
There is an
os.path.getmtime
function that gives the number of seconds since the epochand should be faster than
os.stat
.这是我的版本:
首先,我们构建一个文件名列表。 isfile() 用于跳过目录; 如果应包含目录,则可以省略它。 然后,我们使用修改日期作为键对列表进行就地排序。
Here's my version:
First, we build a list of the file names. isfile() is used to skip directories; it can be omitted if directories should be included. Then, we sort the list in-place, using the modify date as the key.
这是一个单行代码:
它调用 os.listdir() 来获取文件名列表,然后为每个文件名调用 os.stat() 来获取创建时间,然后根据创建时间进行排序。
请注意,此方法仅对每个文件调用 os.stat() 一次,这比为排序中的每个比较调用它更有效。
Here's a one-liner:
This calls os.listdir() to get a list of the filenames, then calls os.stat() for each one to get the creation time, then sorts against the creation time.
Note that this method only calls os.stat() once for each file, which will be more efficient than calling it for each comparison in a sort.
在Python 3.5+中
In python 3.5+
不改变目录:
Without changing directory:
或
或
其中 m 时间是修改时间。
or
or
where m time is modified time.
如果您想按日期顺序读取具有特定扩展名的文件(Python 3),这是我使用不带过滤器的 glob 的答案。
Here's my answer using glob without filter if you want to read files with a certain extension in date order (Python 3).
您可以使用 os.walk('.').next()[-1] 而不是使用 os.path.isfile 进行过滤,但这会在列表,并且 os.stat 将失败。
You could use
os.walk('.').next()[-1]
instead of filtering withos.path.isfile
, but that leaves dead symlinks in the list, andos.stat
will fail on them.为了保证 os.scandir 的完整性(比
pathlib
快 2 倍):For completeness with
os.scandir
(2x faster overpathlib
):这是学习的基本步骤:
this is a basic step for learn:
如果文件是指向不存在文件的符号链接,Alex Coventry 的答案将产生异常,以下代码更正该答案:
当文件不存在时,使用 now(),并且符号链接将位于文件的最末尾。列表。
Alex Coventry's answer will produce an exception if the file is a symlink to an unexistent file, the following code corrects that answer:
When the file doesn't exist, now() is used, and the symlink will go at the very end of the list.
这是我的版本:
This was my version:
这是几行简单的代码,用于查找扩展名并提供排序选项
Here is a simple couple lines that looks for extention as well as provides a sort option
在路径中添加文件目录/文件夹,如果想要特定的文件类型,请添加文件扩展名,然后按时间顺序获取文件名。
这对我有用。
Add the file directory/folder in path, if you want to have specific file type add the file extension, and then get file name in chronological order.
This works for me.
结果 os.listdir 按上次修改顺序排序,但相反,因此您可以执行以下操作:
Turns out
os.listdir
sorts by last modified but in reverse so you can do:也许你应该使用 shell 命令。 在 Unix/Linux 中,find pipelined 和 sort 可能可以做你想做的事情。
Maybe you should use shell commands. In Unix/Linux, find piped with sort will probably be able to do what you want.