Python 按日期顺序全局搜索文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23430395/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 02:58:08  来源:igfitidea点击:

Glob search files in date order?

pythondatesearchglob

提问by Jason Rogers

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

我的python脚本中有这行代码。它在特定目录中的所有文件中搜索 * 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?

有没有办法强制代码按日期顺序搜索?

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

已向 php 询问此问题,但我不确定其中的差异。

Thanks

谢谢

采纳答案by jfs

To sort files by date:

按日期排序文件:

import glob
import os

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

See also Sorting HOW TO.

另请参阅排序方法

回答by Dylan Lawrence

Using glob no. Right now as you're using it, glob is storing all the files simultaneously in code and has no methods for organizing those files. If only the final result is important, you could use a second loop that checks the file's date and resorts based on that. If the parse order matters, glob is probably not the best way to do this.

使用 glob 号 现在,当您使用它时,glob 将所有文件同时存储在代码中,并且没有组织这些文件的方法。如果只有最终结果很重要,您可以使用第二个循环来检查文件的日期和基于此的度假村。如果解析顺序很重要,则 glob 可能不是执行此操作的最佳方法。

回答by luk32

Well. The answer is nope. globuses os.listdirwhich is described by:

好。答案是否定的。glob用途os.listdir描述如下:

"Return a list containing the names of the entries in the directory given by path. The list is in arbitrary order. It does not include the special entries '.' and '..' even if they are present in the directory."

"返回一个包含路径给定目录中条目名称的列表。该列表按任意顺序排列。它不包括特殊条目 '.' 和 '..' 即使它们存在于目录中。

So you are actually lucky that you got it sorted. You need to sort it yourself.

所以你真的很幸运,你把它排序了。你需要自己排序。

This works for me:

这对我有用:

import glob
import os
import time

searchedfile = glob.glob("*.cpp")
files = sorted( searchedfile, key = lambda file: os.path.getctime(file))

for file in files:
 print("{} - {}".format(file, time.ctime(os.path.getctime(file))) )

Also note that this uses creation time, if you want to use modification time, the function used must be getmtime.

还要注意这里使用的是创建时间,如果要使用修改时间,使用的函数必须是getmtime.

回答by Tom

You can sort the list of files that come back using os.path.getmtimeor os.path.getctime. See this other SO answerand note the comments as well.

您可以使用os.path.getmtime或 对返回的文件列表进行排序os.path.getctime。请参阅this other SO answer并注意评论。

回答by Pablo Reyes

Essentially the same as @jfs but in one line using sorted

本质上与@jfs 相同,但在一行中使用 sorted

import os,glob
searchedfiles = sorted(glob.glob("*cycle*.log"), key=os.path.getmtime)

回答by marko.ristin

If your paths are in sortable order then you can always sort them as strings (as others have already mentioned in their answers).

如果您的路径是可排序的,那么您始终可以将它们作为字符串进行排序(正如其他人在他们的答案中已经提到的那样)。

However, if your paths use a datetime format like %d.%m.%Y, it becomes a bit more involving. Since strptimedoes not support wildcards, we developed a module datetime-globto parse the date/times from paths including wildcards.

但是,如果您的路径使用类似 的日期时间格式%d.%m.%Y,则它会变得更加复杂。由于strptime不支持通配符,我们开发了一个模块datetime-glob来解析包含通配符的路径中的日期/时间。

Using datetime-glob, you could walk through the tree, list a directory, parse the date/times and sort them as tuples (date/time, path).

使用datetime-glob,您可以遍历树,列出目录,解析日期/时间并将它们排序为元组(date/time, path)

From the module's test cases:

从模块的测试用例:

import pathlib
import tempfile

import datetime_glob

def test_sort_listdir(self):
    with tempfile.TemporaryDirectory() as tempdir:
        pth = pathlib.Path(tempdir)
        (pth / 'some-description-20.3.2016.txt').write_text('tested')
        (pth / 'other-description-7.4.2016.txt').write_text('tested')
        (pth / 'yet-another-description-1.1.2016.txt').write_text('tested')

        matcher = datetime_glob.Matcher(pattern='*%-d.%-m.%Y.txt')
        subpths_matches = [(subpth, matcher.match(subpth.name)) for subpth in pth.iterdir()]
        dtimes_subpths = [(mtch.as_datetime(), subpth) for subpth, mtch in subpths_matches]

        subpths = [subpth for _, subpth in sorted(dtimes_subpths)]

        # yapf: disable
        expected = [
            pth / 'yet-another-description-1.1.2016.txt',
            pth / 'some-description-20.3.2016.txt',
            pth / 'other-description-7.4.2016.txt'
        ]
        # yapf: enable

        self.assertListEqual(subpths, expected)