Image of Txt Files in Folder
I currently possess a folder with .txt files that are named "Month day.txt"
Running os.listdir('.') outputs in this order:
["April 30.txt", "April 4.txt", "May 1.txt", "May 10.txt", "May 11.txt", "May 2.txt"]
The order I would like to output:
["April 4.txt", "April 30.txt", "May 1.txt", "May 2.txt", "May 10.txt", "May 11.txt"]
To get it, I have a feeling to somehow sort the names by numbers first and then alphabetical.
I spent the last hour researching similar problems, and the best I was able to find was this.
files = os.listdir('.')
re_pattern = re.compile('.+?(\d+)\.([a-zA-Z0-9+])')
files_ordered = sorted(files, key=lambda x: int(re_pattern.match(x).groups()[0]))
Which I understood used a regex to capture the digits and used it as a key for sorting,
which sensibly arranged the files based on the numbers only. (May 12, April 13, etc)
Furthermore, I tried to mess around with it using capture groups on the month, but to no avail.