aaa = ['row_163.pkl', 'row_394.pkl', 'row_679.pkl', 'row_202.pkl', 'row_1449.pkl', 'row_247.pkl', 'row_1353.pkl', 'row_749.pkl', 'row_1293.pkl', 'row_1304.pkl', 'row_78.pkl', 'row_532.pkl', 'row_9.pkl', 'row_1435.pkl']
sorted(aaa, key=lambda x: int(os.path.splitext(x.split('_')[1])[0]))
As In case of mine requirement I have the case like row_163.pkl
here os.path.splitext('row_163.pkl')
will break it into ('row_163', '.pkl')
so need to split it based on '_' also.
but in case of your requirement you can do something like
sorted(aa, key = lambda x: (int(re.sub('\D','',x)),x))
where
aa = ['run01', 'run08', 'run11', 'run12', 'run13', 'run14', 'run18']
and also for directory retrieving you can do sorted(os.listdir(path))
and for the case of like 'run01.txt'
or 'run01.csv'
you can do like this
sorted(files, key=lambda x : int(os.path.splitext(x)[0]))