My goal is to have a task running in the background that sends any changes to a file to my discord server. This is what I've written:
async def readfile():
global carlson
await carlson.wait_until_ready()
server_channel = carlson.get_channel(channel_id_goes_here)
# above code gets channel id once the bot is fully connected
while True:
file = os.open('testfile', os.O_NONBLOCK | os.O_RDWR)
os.lseek(file, 0, 0)
fileread = os.read(file, os.path.getsize(file)).decode()
if fileread != '':
await server_channel.send(fileread) # sends changes to the server
os.write(file, ''.encode()) # seems to append to the file, I want to overwrite what's already there
os.close(file)
asyncio.get_event_loop().create_task(readfile()) # makes code asynchronous
Here's what happened when I set the empty string im writing to the file to 'a', and manually wrote the text 'hola' to the file:
I've even checked the file, and sure enough, the new value is 'holaaaaaaaaaaaa'. I'm unsure how to overwrite the file instead of appending to it. I can't use open() to open the file, as that blocks the rest of the program.