当文件以写模式打开时,文件的已有内容被删除
file = open("newfile.txt", "r") print("初始化读写内容") print(file.read()) print("完成") file.close() file = open("newfile.txt", "w") file.write("新的内容") file.close() file = open("newfile.txt", "r") print("写入新的内容") print(file.read()) print("结束") file.close() 结果 >>>