'How to include a dynamic date in a string

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!



Solution 1:[1]

Does this solve your problem?

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

Solution 2:[2]

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)

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 Januka samaranyake
Solution 2 Gabriel Doretto