标签:操作文件 文字 print 列表 mod == line 字符 flush
# Python learning
# coding:utf-8
# 1.读相关操作
# f.readline() 读一行
# with open(r"a.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)
# f.readlines() 读多行组成一个列表
# res = f.readlines()
# print(res)
# 2.写相关操作
# f.writelines() 从列表中循环写入,写入内容只能是str
l1 = [‘111‘, ‘222‘, ‘333‘]
# with open(r"c.txt", mode="wt", encoding="utf-8") as f:
# res = f.writelines(l1)
l2 = [b‘111aaa‘, b‘222bbb‘, b‘333ccc‘] # 纯英文字符
with open(r"c.txt", mode="wb") as f:
res = f.writelines(l2)
l3 = [‘哈哈哈aaa‘.encode("utf-8"), ‘哈哈哈bbb‘.encode("utf-8"), ‘哈哈哈ccc‘.encode("utf-8")] # 中文字符字符
with open(r"c.txt", mode="wb") as f:
res = f.writelines(l3)
# 3.f.flush 刷新 将写入的内容刷新到硬盘
with open("c.txt", mode="wt", encoding="utf-8") as f:
f.write("哈哈哈")
f.flush()
标签:操作文件 文字 print 列表 mod == line 字符 flush
原文地址:https://www.cnblogs.com/wyless/p/14930440.html