ファイル名に実行時刻を付与したいときによく使うのでメモとして書いておきます。
参考にしたのはsamuraさんの記事。-> https://www.sejuku.net/blog/23606
以下では日時のスタンプを付与してます。
timestamp.py
# Import
import datetime
# Create Time Stamp using zero fill
date = datetime.datetime.now()
time_stamp = '%04d%02d%02d' % (date.year, date.month ,date.day)
print(time_stamp)
利用例
create_file.py
f = open('sample%s.txt' % time_stamp ,'w')
f.write( str(time_stamp) + '\n')
f.close()
年から秒まで以下の方法で取得可能
https://www.sejuku.net/blog/23606
samples
print("年:",date.year)
print("月:",date.month)
print("日:",date.day)
print("時:",date.hour)
print("分:",date.minute)
print("秒:",date.second)