import pathlib print(pathlib.Path('yourPath.example').suffix) # '.example' #or import os filename, file_extension = os.path.splitext('/path/to/somefile.ext')