How do you get a directory listing sorted by creation date in python?

前端 未结 17 1370
忘了有多久
忘了有多久 2020-11-22 15:14

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?

17条回答
  •  粉色の甜心
    2020-11-22 15:54

    from pathlib import Path
    import os
    
    sorted(Path('./').iterdir(), key=lambda t: t.stat().st_mtime)
    

    or

    sorted(Path('./').iterdir(), key=os.path.getmtime)
    

    or

    sorted(os.scandir('./'), key=lambda t: t.stat().st_mtime)
    

    where m time is modified time.

提交回复
热议问题