Reputation: 290
I'm working on a discord command, that writes the whole text file into the chat line by line, and I tried making it but somewhy It doesn't work properly.
file = open('story.txt', 'r')
@client.command(alisases = ['readfile'])
async def story(ctx):
for x in file:
await ctx.send(file)
It runs, but only writes these lines:
<_io.TextIOWrapper name='story.txt' mode='r' encoding='cp1250'>
Upvotes: 0
Views: 713
Reputation: 1291
You are sending the string representation of the file object rather than the lines in it.
You could do something like this:
@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)
Also, it's a good practice to use the with open
syntax as it assures that the file is being closed properly.
Upvotes: 2