# 读取已存在的某个文件内的内容
f = open('note.txt', 'r', encoding='utf-8')
s = f.readlines()
print(s)
f.close()
# 推荐使用以下方式 with...as :上下文管理
with open('note.txt', 'r', encoding='utf-8') as f:
print(f.readlines())
# 向文件中写入内容
with open('ab.txt','a+',encoding='utf-8') as file:
file.write('你好\n我好\n大家好\n写入练习----2022.6.15')
本文介绍了如何使用Python通过`open()`函数读取'note.txt'文件的内容,并演示了两种文件操作方式:逐行读取和with语句。随后展示了如何向'ab.txt'文件写入字符串,重点讲解了追加模式'a+'的用法。
1494

被折叠的 条评论
为什么被折叠?



