If you just want to get the file name without the extension from a file path then you can make use of the PurePath.stem method from the pathlib module.
Examples:
>>> from pathlib import PurePath
>>>
>>> PurePath('my/data_compressed.tar.gz').stem
'data_compressed.tar'
>>>
>>> PurePath('my/sales.xlsx').stem
'sales'
>>>
>>> PurePath('my/2023_data.csv').stem
'2023_data'
>>>

Example:
from pathlib import PurePath
data_file = '/mnt/data/2023.csv'
year_file = PurePath(data_file)
year = year_file.stem
print(year)
Output:
Provide Feedback For This Article
We take your feedback seriously and use it to improve our content. Thank you for helping us serve you better!
😊 Thanks for your time, your feedback has been registered!
Comments & Discussion
Facing issues? Have questions? Post them here! We're happy to help!