2

I'm trying to send a text file with discord.py, but when I send the file, it seems to be empty.

An example snippet:

bytes = BytesIO()
test = b'sadfasdfsa'
bytes.write(test)
print(bytes)
await ctx.send(file=File(bytes, 'test.txt'))

This results in an empty test.txt file getting sent. Why is it not sending the contents?

cclloyd
  • 8,171
  • 16
  • 57
  • 104

1 Answers1

2

After you call BytesIO.write() the BytesIO pointer will be set to the end of the file. To read the full file from memory you need to set the pointer to the beginning of the file first using seek(). So before sending the file you would call:

bytes_.seek(0)

On another note, if you name your variable bytes you will overwrite the built-in Python type, which is why I renamed it in my example above.

elyas
  • 1,355
  • 6
  • 12