dfpaezo
dfpaezo

Reputation: 9

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?

Upvotes: 1

Views: 74

Answers (2)

Johnny John Boy
Johnny John Boy

Reputation: 3202

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')

Upvotes: 1

manjy
manjy

Reputation: 109

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.

Upvotes: 0

Related Questions