Reputation: 67
I need to read an excel where there are some folders and each folder contains many files in the location and my desired file will be in one of those folders. How I can read my desired file from that location? My file name is like Daily Report on (18-Nov-2020)_ALL.xlsm
and I want to read as df1 = pd.read_excel(r'C:\(many folders here)\Daily Report on (18-Nov-2020)_ALL.xlsm',header = 1, usecols = "A:B,D:N,U,W,Z",skiprows = range(1,3))
.
The date part of my file name 18-Nov-2020
is not fixed and it is the current date of system. How can I change this date part in file name automatically everyday while run the program?
Looking forward to your support
Upvotes: 1
Views: 61
Reputation: 563
glob
module. '**'
means 'all possible directories and subdirectories' Also make sure, recursive=True
from glob import glob
import os
today_file_path = glob(os.path.join(fixed_path, '**', today_filename), recursive=True)[0]
%d
is day of the month, %b%
is shortened name of the Month, %Y
is yearfrom datetime import date
today_formatted = date.today().strftime('%d-%b-%Y')
today_filename = 'Daily Report on ({})_ALL.xlsm'.format(today_formatted)
print(today_filename)
output:
'Daily Report on (28-Nov-2020)_ALL.xlsm'
Upvotes: 1