split file name and extension pathlib has you covered, just make a Path object and access the stem and suffix attributes: import pathlib path = pathlib.Path('20240711112258.md') print(path.stem) # 20240711112258 print(path.suffix) # .md #pathlib