Extracting File Extensions in Python with pathlib and os.path
The most reliable way to get a file extension in Python is os.path.splitext(), which splits a filename into the base name and extension. For modern code, pathlib.Path provides a cleaner alternative. Using os.path.splitext() os.path.splitext() returns a tuple of (name, extension): import os name, ext = os.path.splitext(‘file.txt’) print(name) # ‘file’ print(ext) # ‘.txt’ This works with…
