1. 文件不存在,创建文件后写出方法: <1>打开文件:open()方法是文件不存在时创建文件 file= open("D:/test.txt","w",encoding="UTF-8") <2>写出文件: file.write("please open your book") #内容写到内存中 <3>关闭文件 flush 刷新方法,刷新才能写到硬盘的文件中 file.flush() time.sleep(10) # 睡眠10秒
2. 存在的文件写出方法:
file=open("D:/python.txt","w",encoding="UTF-8") #打开已存在的文件 file.write("hello world!!!") #写出文件 file.close() #关闭文件