1、第一,print()输出到txt文档。运行下列代码,就可以把“Where there is a will, there is a way”输入到txt文档中。fi造婷用痃leone = open(r'D:\test.txt','a+')print('Where there is a will, there is a way',file=fileone)fileone.close()

3、第三,如果输出年份,这需要调用datetime模块。运行下列代码,可以将当前年份输出出来。坡纠课柩import dateti罪焐芡拂mefiletwo = open(r'D:\test.txt','a+')print('当前年份:'+str(datetime.datetime.now().year),file = filetwo)filetwo.close()

5、第五,要想输入年月日和时分秒,只需要在上述代码的基础的增加如下代码:print('当前日期时间:'+datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'),file = filetwo)
