如果往文件写入的是字符串,则在with语句缩进内使用.write()
如果往文件写入的是字符串列表,则使用.writelines()
尝试在右边代码框完成以下操作:
b=["\n\n寻梦?撑一支长篙,","\n向青草更青处漫溯;","\n满载一船星辉,","\n在星辉斑斓里放歌。"]
with open("/data/poetry.txt","a+",encoding="utf-8") as file:
file.writelines(b)
file.seek(0)
print(file.read())