from pathlib import Path print(Path(__file__).stem) #myfile print(Path(__file__).name) #myfile.py
Here is what the above code is Doing:
1. __file__ is a global variable that contains the path to the current file.
2. Path(__file__) creates a Path object from the path in __file__.
3. Path(__file__).stem returns the stem of the path, which is everything except for the file extension.
4. Path(__file__).name returns the name of the path, which is everything including the file extension.