-1๐
โ
If you need to match the exact extension:
# (?<=/) ensure that before the match is /
# [^/]*.xlsx search for anything but / followed by .xlsx
mo1 = re.search('(?<=/)[^/]*.xlsx', path).group(0)
print(mo1)
My_file.xlsx
otherwise:
path='/home/user/Desktop/My_file.xlsx'
with regex:
mo = re.search(r'(?<=/)([\w.]+$)',path)
print(mo.group(1))
My_file.xlsx
with rsplit:
my_file = path.rsplit('/')[-1]
print(my_file)
My_file.xlsx
๐คLetzerWille
2๐
If you know that the file extension is always the same (e.g. โ.xlsxโ) I would suggest you to go this way:
import os
filename_full = os.path.basename(path)
filename = filename_full.split(".xlsx")[0]
Hope it helps
๐คFSp
- How do you perform a django lookup between different applications using the same database?
- Can't find "Set as Django Project" or Django actions in Eclipse with Pydev
- Reverse for 'product_list_by_category' with arguments '('books',)' and keyword arguments '{}' not found
Source:stackexchange.com