Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Directory Listing based on time [duplicate]

How to list the files in a directory based on timestamp?

 os.listdir() 

lists in arbitrary order.

Is there a build-in function to list based on timestamp? or by any order?

like image 563
vkris Avatar asked Dec 21 '10 14:12

vkris


1 Answers

You could call stat() on each of the files and sort by one of the timestamps, perhaps by using a key function that returns a file's timestamp.

import os

def sorted_ls(path):
    mtime = lambda f: os.stat(os.path.join(path, f)).st_mtime
    return list(sorted(os.listdir(path), key=mtime))

print(sorted_ls('documents'))
like image 122
HarryM Avatar answered Oct 19 '22 07:10

HarryM