#一、读相关
#1、readline:一次读一行
# with open(r'g.txt',mode='rt',encoding='utf-8') as f:
# # res1=f.readline()
# # res2=f.readline()
# # print(res2)
#
# while True: #一次读一行
# line=f.readline()
# if len(line) == 0:
# break
# print(line,end='')
#2、readlines:
# with open(r'g.txt',mode='rt',encoding='utf-8') as f:
# res=f.readlines()
# print(res)
#强调:
#f.read 与f.readlines()都是讲内容一次性读入内存,如果内容过多过大
#会导致内存占用过大
#二、写相关
#f.writelines():
# with open(r'h.txt',mode='wt',encoding='utf-8') as f:
# # f.write('111\n222\n333\n')
#
# l=['1111\n','2222\n','3333\n']
# # for line in l:
# # f.write(line)
#
# f.writelines(l) #相当于上面那个for循环
# with open(r'h.txt',mode='wb') as f:
# l=[
# '1111\n'.encode('utf-8'),
# '2222\n'.encode('utf-8'),
# '3333\n.'.encode('utf-8')
# ]
#补充一:写进去的内容时纯英文字符不用加【.encode('utf-8')】,直接在前面加上 b 也能得到bytes类型
# l=[
# b'1111\n',
# b'2222\n',
# b'3333\n.'
# ]
#补充二:'上'.encde('utf-8')等同于bytes('上’,encoding='utf-8')
# l=[
# bytes('上啊',encoding='utf-8'),
# bytes('冲啊',encoding='utf-8'),
# bytes('荣望啊',encoding='utf-8'),
# ]
# f.writelines(l)
'''
>>> '上'.encode('utf-8')
b'\xe4\xb8\x8a'
>>> bytes('上',encoding='utf-8')
b'\xe4\xb8\x8a'
'''
#3、flush:将内存的数据立即写进硬盘
# with open(r'h.txt',mode='wt',encoding='utf-8') as f:
# f.write('哈哈哈')
# f.flush()
#了解: