How can I generate a file in python with today's date? -
i have seen several post nobody answer question straight point.
im creating file in python this:
f = open('myfile.extension','w')
what should add line add date file generated?
im using import time , can current date in other part of script, dont know how add date...
thank you
assuming trying add date filename
datetime import datetime datestring = datetime.strftime(datetime.now(), '%y/%m/%d_%h:%m:%s') f = open('myfile_'+datestring+'.extension', 'w')
you can change format like. above print out datestring
so:
datetime.strftime(datetime.now(), '%y/%m/%d_%h:%m:%s') '2015/08/07_16:07:37'
of course since filename may not want have /
, recommend format following:
datetime.strftime(datetime.now(), '%y-%m-%d-%h-%m-%s') '2015-08-07-16-07-37'
here's full run of of above:
>>> datetime import datetime >>> datestring = datetime.strftime(datetime.now(), '%y-%m-%d-%h-%m-%s') >>> f = open('myfile_' + datestring + '.ext', 'w') >>> f.name 'myfile_2015-08-07-16-24-23.ext'
Comments
Post a Comment