0

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!

spcol
  • 437
  • 4
  • 15
  • 1
    Does this answer your question? [Convert datetime object to a String of date only in Python](https://stackoverflow.com/questions/10624937/convert-datetime-object-to-a-string-of-date-only-in-python) – Chris Apr 14 '22 at 15:34
  • 1
    Did the above give an error? What was it? There are several options for string formatting here: https://realpython.com/python-string-formatting/ – Sarah Messer Apr 14 '22 at 15:34

2 Answers2

2

Does this solve your problem?

from datetime import date
    
morningupdate.to_csv(f'morningupdate_{date.today()}.csv')
Januka samaranyake
  • 2,385
  • 1
  • 28
  • 50
0

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)