Reputation: 29
I have a few files stored in my directory according to their dates such as
01mar13
09mar13
20feb13
27jan13
my problem is I want to sort them such that its arranged as
27jan13
20feb13
01mar13
09mar13
I have a feeling it should be very simple. Would appreciate if someone could point me to the right direction of solving my problem.
Upvotes: 1
Views: 136
Reputation: 213115
from datetime import datetime
import glob
sorted(glob.glob('*'), key=lambda x: datetime.strptime(x, '%d%b%y'))
returns a list of filenames sorted chronologically (centuries ignored). And yes, storing files in YYYYMMDD format is a good solution.
Upvotes: 3