1.11M Members

Add Current Date to a File Name

 
0
 

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.

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

Thanks.

 
0
 

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.

Question Answered as of 4 Years Ago by griswolf
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article