方法一:
with open("test.txt","w") as file:
file.write("hello")
注释:
1.open()函数第一个参数为文件名
如果文件不存在将创建文件,若文件存在则覆盖原文件内容
2.open()第二个参数为文件的读写模式,详情请查看python基础教程
方法二:
file2 = open("test2.txt","w")
file2.write("hello2")
file2.close()
注释:
此方法使用时不要忘了在最后加上file2.close()
close函数作用是关闭文件,防止内存溢出