I think the easiest way is to use .split("/")
input= "PATH/TO/FILE/file.txt"
file_only = input.split("/")[-1]
print(file_only)
>>> file.txt
You can also do this to extract the last folder:
input= "PATH/TO/FOLDER"
folder_only = input.split("/")[-1]
print(folder_only)
>>> FOLDER
If you want the penultimate folder, simply change [-1] to [-2].