Home > OS >  How to include a dynamic date in a string
How to include a dynamic date in a string

Time:04-15

I'm trying to write a daily csv file but would like the title of the csv file to specify today's date.

It seems to be failing every time but I'm sure there's an easy way to do this..? Hopefully this isn't a duplicate but can't seem to find another question similar.

At the minute I've just tried this;

from datetime import date

morningupdate.to_csv('morningupdate'   '_'   date.today() '.csv')

My brain is completely broken with this, any help much appreciated!

CodePudding user response:

Does this solve your problem?

from datetime import date
    
morningupdate.to_csv(f'morningupdate_{date.today()}.csv')

CodePudding user response:

You are trying to concatenate string with a datetime object.

You can use f string to manage the problem:

from datetime import date

path = f'morningupdate_{date.today()}.csv'
morningupdate.to_csv(path)
  • Related