I have a list of filenames called filelist
In []: filelist
Out []: [\'C:\\\\Mon20412\\\\P-2NODE-RAID6-1BLACK-32k-100-segmented.xlsx\',
Use a regex to parse out the number and use that as a sort key.
Quick and dirty:
import re
l = ['C:\\Mon20412\\P-2NODE-RAID6-1BLACK-32k-100-segmented.xlsx',
'C:\\Mon25312\\P-2NODE-RAID6-13RED-32k-100-segmented.xlsx',
'C:\\Mon20362\\P-2NODE-RAID6-2GREEN-32k-100-segmented.xlsx']
def get_sort_number(s):
pattern = r'C:\\Mon\d+\\P-2NODE-RAID6-(\d+)'
try:
return int(re.match(pattern, s).group(1))
except AttributeError:
return 0
sorted(l, key=get_sort_number)
This gives
['C:\\Mon20412\\P-2NODE-RAID6-1BLACK-32k-100-segmented.xlsx',
'C:\\Mon20362\\P-2NODE-RAID6-2GREEN-32k-100-segmented.xlsx',
'C:\\Mon25312\\P-2NODE-RAID6-13RED-32k-100-segmented.xlsx']
All strings that cannot be matched by the regex would be at the beginning of the sorted list.