'How to get path with a string filename with digits within in Python?

I need to download a file that is automated on SharePoint. The thing is this file has the following filename structure:

fileYYYYMmm.xlsb

Example: file2022M03.xlsb

I must refer to this file using a wildcard or something (I don't know what exactly), to get dynamically that file.

Example: file????M??.xlsb

I'm using the following line code

download_path = sp.create_link(f'https://enterprise.sharepoint.com/:x:/r/sites/GLB-GIS-PERISCPE/Shared%20Documents/TMS_Ch/file/file'+str(yy)+'M'+'??'+'.xlsb')

How can I do this in Python?



Solution 1:[1]

It's pretty easy with an f string. You just need to reference the variable in curly braces like this:

the_year = '22'
the_month = '03'

# print(f'https://enterprise.sharepoint.com/:x:/r/sites/GLB-GIS-PERISCPE/Shared%20Documents/TMS_Ch/file/file{the_year}M{the_month}.xlsb')
# https://enterprise.sharepoint.com/:x:/r/sites/GLB-GIS-PERISCPE/Shared%20Documents/TMS_Ch/file/file22M03.xlsb

download_path = sp.create_link(f'https://enterprise.sharepoint.com/:x:/r/sites/GLB-GIS-PERISCPE/Shared%20Documents/TMS_Ch/file/file{the_year}M{the_month}.xlsb')

Solution 2:[2]

I am assuming you only need one file and you will have variable name for year and month

You can use F-string like this:

download_path = sp.create_link(f'https://enterprise.sharepoint.com/:x:/r/sites/GLB-GIS-PERISCPE/Shared%20Documents/TMS_Ch/file/file{yy_var}M{mm_var}.xlsb')

Here yy_var will have store the year and mm_var will store the month.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Johnny John Boy
Solution 2 manjy