I have files with names like this:
'aaaa 4b 123b.txt'
'aaaa 4b 124b.txt'
'aaaa 4b 125b.txt'
'aaaa 4b 126b.txt'
'aaaa 4b 127b.txt'
'aaaa 4b 128b.txt'
'aaaa 4b 129 (123c)b.txt'
'aaaa 4b 129 (124c)b.txt'
'aaaa 4b 129 (125c)b.txt'
'aaaa 4b 129 (126c)b.txt'
'aaaa 4b 129 (127c)b.txt'
'aaaa 4b 129b.txt'
'aaaa 4b 130b.txt'
'aaaa 4b 131b.txt'
'aaaa 4b 132b.txt'
Each files names are stored in a list using os.listdir(path)
The above is how the files should be sorted, but my files are, in fact, not sorted at all. The parenthesis inside the file names in 129-series makes the issue complicated. If there were no parenthesis in some of the file names, I can sort using,
List.sort(key = lambda x: int(re.search('([0-9]+)(b.txt)', x).group(1)))
But How can I make exceptions for files that have parenthesis, and sort everything at once?
Edit: Original (unsorted list)
['aaaa 4b 128b.txt', 'aaaa 4b 127b.txt', 'aaaa 4b 129 (127c)b.txt', 'aaaa 4b 131b.txt', 'aaaa 4b 123b.txt', 'aaaa 4b 129 (125c)b.txt' ...]
How I want it to be:
['aaaa 4b 123b.txt', 'aaaa 4b 124b.txt', 'aaaa 4b 125b.txt' ... 'aaaa 4b 128b.txt', 'aaaa 4b 129 (124c)b.txt', 'aaaa 4b 129 (125c)b.txt', ... 'aaaa 4b 131b.txt', 'aaaa 4b 132b.txt']