amazingbo 2019-12-24
name = "wangyang" age = "25" with open("C:/Users/mike1/Desktop/name_age.txt", "w", encoding = "utf-8") as f1: f1.write("hellow world")
这么写是不行的,文件名是name_age.txt,而不是wangyang_25.txt.
如下图:
正确的方式应该是用format()函数
name = "wangyang" age = "25" with open("C:/Users/mike1/Desktop/{0}_{1}.txt".format(name, age), "w", encoding = "utf-8") as f1: f1.write("hellow world")
如下图所示:
关于format() 的一些基本的用法: