python新建txt文件并以当前时间命名

python新建txt文件并以当前时间命名,第1张

代码内容:

python新建txt文件并以当前时间命名,在文件中写入当前时间,追加数组,在写入数组后,在文末写入ok!,并在执行栏中输出open the file!


代码内容:
from datetime import datetime

list=[0.268,0.6589,3.4127,4.23650,2.455]
NowOnTxt = datetime.strftime(datetime.now(),'%m-%d %H:%M:%S')
now=datetime.strftime(datetime.now(),'%m%d %H%M%S')
fname='./'+now+'joints.txt'
#with open(fname,'a') a是追加 w是覆盖
with open(fname,'a') as f:
    f.write('time: '+str(NowOnTxt)+'\n')
    for i in range(len(list)):
        f.write('{:.3f}\n'.format(list[i]))
    f.write('ok!')
print('open the file!')

输出结果:

执行栏:

文件目录新生成文件:

txt文件内容:

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/langs/922038.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-16
下一篇2022-05-16

发表评论

登录后才能评论

评论列表(0条)

    保存