How to get a directory list sorted by date?

Joel Goldstick joel.goldstick at gmail.com
Sun May 15 10:45:00 EDT 2016


On Sun, May 15, 2016 at 10:36 AM, Grant Edwards
<grant.b.edwards at gmail.com> wrote:
> On 2016-05-15, Tim Chase <python.list at tim.thechases.com> wrote:
>> On 2016-05-15 11:46, Peter Otten wrote:
>>> def sorted_dir(folder):
>>>     def getmtime(name):
>>>         path = os.path.join(folder, name)
>>>         return os.path.getmtime(path)
>>>
>>>     return sorted(os.listdir(folder), key=getmtime, reverse=True)
>>>
>>> The same idea will work with pathlib and os.scandir():
>>>
>>> def _getmtime(entry):
>>>     return entry.stat().st_mtime
>>>
>>> def sd_sorted_dir(folder):
>>>     return sorted(os.scandir(folder), key=_getmtime, reverse=True)
>>
>> unless sorted() returns a lazy sorter,
>
> What's a lazy sorter?

Some postal workers?   just kidding
>
> --
> Grant
>
> --
> https://mail.python.org/mailman/listinfo/python-list



-- 
Joel Goldstick
http://joelgoldstick.com/blog
http://cc-baseballstats.info/stats/birthdays



More information about the Python-list mailing list