为了账号安全,请及时绑定邮箱和手机立即绑定

Discord Bot 逐行发送文本文件

Discord Bot 逐行发送文本文件

繁星点点滴滴 2022-11-09 16:43:22
我正在研究一个不和谐的命令,它将整个文本文件逐行写入聊天中,我尝试制作它,但不知何故它不能正常工作。    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' 模式='r' 编码='cp1250'>
查看完整描述

1 回答

?
冉冉说

TA贡献1877条经验 获得超1个赞

您正在发送文件对象的字符串表示,而不是其中的行。


你可以这样做:


@client.command(alisases = ['readfile'])

async def story(ctx):

    with open('story.txt', 'r') as story_file:

        for line in story_file:

            await ctx.send(line)

此外,使用with open语法是一个很好的做法,因为它可以确保文件被正确关闭。


查看完整回答
反对 回复 2022-11-09
  • 1 回答
  • 0 关注
  • 124 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号