我正在使用discord命令,它将整个文本文件逐行写入聊天室,我尝试使用它,但不知为什么它不能正常工作
file = open('story.txt', 'r')
@client.command(alisases = ['readfile'])
async def story(ctx):
for x in file:
await ctx.send(file)
它运行,但只写入以下行:
<_io.TextIOWrapper name='story.txt'mode='r'encoding='cp1250'>
Tags:
您发送的是文件对象的字符串表示形式,而不是其中的行
你可以这样做:
另外,使用
with open
语法是一种很好的做法,因为它可以确保文件被正确关闭相关问题 更多 >
编程相关推荐