Python 基础 读写文件

来源:互联网 发布:win10 软件显示乱码 编辑:程序博客网 时间:2024/06/04 18:02

open 打开文件,当文件不存在时会创建一个空文件
打开后要执行关闭命令,close

text = 'This is my first test.\nThis is next line\nThis is last line.'print(text)my_file = open('my file.txt','w')my_file.write(text)my_file.close()

在打开的文件中追加内容,open的参数为 a

append_text = '\nThis is appended file.my_file = open('my file.txt','a')my_file.write(append_text)my_file.close()

把file里的内容存入content中

file = oprn('myfile.txt','r')content = file.read()print(content)
原创粉丝点击