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

前端 未结 17 1580
忘了有多久
忘了有多久 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:42

    This was my version:

    import os
    
    folder_path = r'D:\Movies\extra\new\dramas' # your path
    os.chdir(folder_path) # make the path active
    x = sorted(os.listdir(), key=os.path.getctime)  # sorted using creation time
    
    folder = 0
    
    for folder in range(len(x)):
        print(x[folder]) # print all the foldername inside the folder_path
        folder = +1
    

提交回复
热议问题