我正在研究一個不和諧的命令,它將整個文本文件逐行寫入聊天中,我嘗試制作它,但不知何故它不能正常工作。 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語法是一個很好的做法,因為它可以確保文件被正確關閉。
添加回答
舉報
0/150
提交
取消