文件操作的过程
1)打开
2)操作
3)关闭
1.写(清空写入)
# f = open(file='test', mode='w', encoding='utf-8') # 第一种情况 # f.write("Hello World!") # f.close() # f = open(file='test', mode='wb') # 第二种情况 # f.write("杨".encode('utf-8')) # f.close() # with open(file='test', mode='w', encoding='utf-8') as f: # 第三种情况 # f.write("高圆") with open(file='test', mode='wb') as f1: # 第四种情况 f1.write("李璐".encode('utf-8'))
2.读
# 读 # f = open(file='test', mode='r', encoding='utf-8') # data = f.read() # print(data) # f.close() # f = open(file='test', mode='rb') # data = f.read() # print(data.decode('utf-8')) # f.close() # with open(file='test', mode='r', encoding='utf-8') as f1: # data = f1.read() # print(data) with open(file='test', mode='rb') as f1: data = f1.read().decode('utf-8') print(data)
3.追加写入
# 追加 # f = open(file='test', mode='a', encoding='utf-8') # f.write('\nWhat?') # f.close() # f = open(file='test', mode='ab') # f.write("\ntom".encode('utf-8')) # f.close() # with open(file='test', mode='a', encoding='utf-8') as f1: # f1.write("\nHi") with open(file='test', mode='ab') as f1: f1.write('\njoker'.encode('utf-8'))
4.读写
# 读写 # f = open(file='test', mode='r+', encoding='utf-8') # data = f.read() # print(data) # f.write("\tok") # f.close() # f = open(file='test', mode='r+b') # data = f.read().decode('utf-8') # print(data) # f.write('\tfun'.encode('utf-8')) # f.close() # with open(file='test', mode='r+', encoding='utf-8') as f1: # data = f1.read() # print(data) # f1.write("\tfine") with open(file='test', mode='r+b') as f1: data = f1.read().decode('utf-8') print(data) f1.write('\tsad'.encode('utf-8'))
5.清空写读
# 清空写读 # f = open(file='test', mode='w+', encoding='utf-8') # f.write("blue") # f.seek(0) # data = f.read() # print(data) # f = open(file='test', mode='w+b') # f.write("joker".encode('utf-8')) # f.seek(0) # data = f.read().decode('utf-8') # print(data) # with open(file='test', mode='w+', encoding='utf-8') as f1: # f1.write("Hello World!") # f1.seek(0) # data = f1.read() # print(data) with open(file='test', mode='w+b') as f1: f1.write("Thank you!".encode('utf-8')) f1.seek(0) data = f1.read().decode('utf-8') print(data)
6.追加写读
# 追加写 # f = open(file='test', mode='a+', encoding='utf-8') # f.write("\ttom") # f.seek(0) # data = f.read() # print(data) # f = open(file='test', mode='a+b') # f.write("\ttom".encode('utf-8')) # f.seek(0) # data = f.read().decode('utf-8') # print(data) # with open(file='test', mode='a+', encoding='utf-8') as f1: # f1.write("\tjoker") # f1.seek(0) # data = f1.read() # print(data) with open(file='test', mode='a+b') as f1: f1.write("\ttom".encode('utf-8')) f1.seek(0) data = f1.read().decode('utf-8') print(data)
7.常用方法
1)read(n)
光标位置m,读取从m->m+n,n个字符
2)seek(n)
设置光标的位置,n为字节(从0开始)
3)tell()
获取当前光标的位置,单位是字节
with open(file='test1', mode='r+', encoding='utf-8') as f: f.seek(3) # 设置光标位置,字节 data = f.read(2) # 读取,字符 a = f.tell() # 获取光标位置,字节 print(data, '\t', a)
4)按行读取
a.for循环
with open(file='test1', mode='r+', encoding='utf-8') as f: for line in f: print(line)
b.readlines
readlines(),打印的是list,一行作为一个元素
with open(file='test1', mode='r+', encoding='utf-8') as f: data = f.readlines() print(data)
c.readline()
读取一行
with open(file='test1', mode='r+', encoding='utf-8') as f: data = f.readline() print(data)
d.truncate(n)
n为空时,从当前位置截断
n不为空时,从n开始截断(n后的内容舍弃)
n为字节
with open(file='test', mode='w', encoding='utf-8') as f: f.write("你好,之华,之南") f.truncate(6)