是否按日期顺序搜索文件? [英] Glob search files in date order?

查看:64
本文介绍了是否按日期顺序搜索文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的python脚本中有这行代码.它在特定目录中的所有文件中搜索* cycle * .log.

I have this line of code in my python script. It searches all the files in in a particular directory for * cycle *.log.

for searchedfile in glob.glob("*cycle*.log"):

这很好用,但是当我将脚本运行到网络位置时,它不会按顺序搜索它们,而是随机搜索.

This works perfectly, however when I run my script to a network location it does not search them in order and instead searches randomly.

有没有一种方法可以强制代码按日期顺序搜索?

Is there a way to force the code to search by date order?

这个问题已经被要求使用php,但是我不确定它们之间的差异.

This question has been asked for php but I am not sure of the differences.

谢谢

推荐答案

按日期对文件进行排序:

To sort files by date:

import glob
import os

files = glob.glob("*cycle*.log")
files.sort(key=os.path.getmtime)
print("\n".join(files))

另请参见排序方法.

这篇关于是否按日期顺序搜索文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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