file=open("python1.txt","r+")
res=file.read()
print(file.write()
print(res)
1: file文件open之后默认是r 只读模式 如果你要写入内容 则会报错
2:r+ 可读可写 先写的划 从头开始覆盖写 读光标之后的内容 读写跟着光标走
3:w 只写 硬要去读 同样也会报错
4:w+ 可读可写 不管是w 还是w+ 如果文件存在 就直接清空 再重写
5如果文件不存在 则新建一个文件 然后写
file=open("python2.txt","r",encoding='utf-8')
print(file.write('888999\n','oppofingx最棒'))
6: a 追加 a+
如果文件存在 就直接追加写 写在后面 如果不存在 则新建一个文件进行结果写入
重点掌握r a
file=open("python1.txt","r",encoding='utf-8') #当文件里有中文字符时需要用到encoding作为字符转换
print(file.read()) 读取所有内容
print(file.readline()) 按行读取,只读取一行
print(file.readlines()) 读取所有行,但是返回的是列表 ['888999\n', 'oppofindx最棒']
file_2=open("python2.txt","a",encoding='utf-8')
print(file_2.write("20200923 file 操作"))
file_2.writelines(["777\n","888"])
Python学习笔记1.21---文件的读写
最新推荐文章于 2023-09-11 10:59:15 发布