
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文件内容:
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)