import datetime # imports the datetime library
x = str(datetime.datetime.now()) # sets a variable x to be the current datatime, converted to string format
with open("data.txt", 'w+') as f: # creates (or opens and will overwrite if already exists) the file data.txt and changes the reference to be f
f.write(x) # writes to data.txt the contents of the x variable
1条答案
按热度按时间plicqrtu1#
您需要的内容如下:
上面代码的问题在于
datetime.datetime.now()
没有返回str
类型,而是返回datetime.datetime
类型,Python不能直接编写该类型。