import re
import math
from pathlib import Path
file_pattern = re.compile(r'.*?(d+).*?')
def get_order(file):
match = file_pattern.match(Path(file).name)
if not match:
return math.inf
return int(match.groups()[0])
sorted_files = sorted(files, key=get_order)