1

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.

4

1 回答 1

3
from datetime import datetime
import glob

sorted(glob.glob('*'), key=lambda x: datetime.strptime(x, '%d%b%y'))

返回按时间顺序排序的文件名列表(忽略世纪)。是的,以 YYYYMMDD 格式存储文件是一个很好的解决方案。

于 2013-04-12T10:34:37.417 回答