Start New Discussion within our Software Development Community

Is there a way to append the current date to an output file. My output statement looks like,

result=open('Results.csv','wb')

If I run it today, I would like the file name to be something like Results20100528 (for May 28, 2010). If I run it tomorrow, I would like it to be Results20100529.

Thank you.

import datetime
now = datetime.datetime.now()
suffix = now.strftime("%Y%m%d_%M%S)
result = open('Results%s.csv'%suffix,'wb')

If anyone reads this in the future, there is one correction to the syntax. There should be end quotes for strftime.

import datetime
now = datetime.datetime.now()
suffix = now.strftime("%Y%m%d_%M%S")
result = open('Results%s.csv'%suffix,'wb')

Thanks again.

This question has already been answered. Start a new discussion instead.