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.

Recommended Answers

All 3 Replies

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

Thanks.

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.