0
我不知道这是为什么不工作如何通过python创建文本文件?
import time
consumption = "300"
spend = "5000"
def create_report(consumption, spend):
date = time.strftime("%d/%m/%Y")
date = date + ".txt"
file = open(date, "w")
file.write("Since: ", pastdate)
file.write("Consumption in £: ", consumption)
file.write("Overall spend in £: ", spend)
file.close()
create_report(consumption, spend)
我希望能够简单地创建一个文本文件,并在它与今天的日期文本文件的名字写。 “w”似乎没有创建文件。我得到的错误:
file = open(date, "w")
FileNotFoundError: [Errno 2] No such file or directory: '01/03/2016.txt'
这不是您平台上的有效文件名。 – jonrsharpe
使用'date = time.strftime(“%d-%m-%Y”)'作为替代 – gtlambert
尝试'w +'而不是'w',看看是否可以解决您的问题。 –