I'm trying to define a function that pulls data out of a newly exported csv every day, where the name of the csv changes based on the day.
So far I have the following:
import pandas as pd
todays_date = pd.to_datetime('today').strftime('%Y%m%d')
todays_date_name_string = 'unchanging part of filename ' str(todays_date)
var1 = fnmatch.filter(os.listdir('P:directory/'), 'todays_date_name_string*.csv')
print(var1)
But an empty list is printed. I can't seem to get it to take the variable even though when I print todays_date_name_string by itself I get the string I want, am I using fnmatch or os.listdir incorrectly?
CodePudding user response:
Change this line:
var1 = fnmatch.filter(os.listdir('P:directory/'), 'todays_date_name_string*.csv')
to
var1 = fnmatch.filter(os.listdir('P:directory/'), f'{todays_date_name_string}*.csv')
Your problem is that you're trying to use the variable todays_date_name_string
, which contains todays date as a string, but you're not actually using it. You're using the string todays_date_name_string
, so you're basically just trying to get all files that start with, literally todays_date_name_string
, and end with .csv
.