** python 文件操作写入write ** # 文件写入操作,w文件不存在创建,文件存在清空 f = open("D:/test.txt", "w", encoding="UTF-8") # write写入 f.write("你好") # flush()刷新 f.flush() # close() 关闭 内置了flush方法 f.close() 运行结果: # 文件追加 f = open("D:/test.txt", "a", encoding="UTF-8") #write写入,flush刷新 f.write("\n小明") f.flush() f.close() 运行结果: