txt文件中写入数据的错误

时间:2018-02-03 13:26:51

标签: python-3.x discord discord.py

PROGRAMM

@client.event
async def on_message(message):
    my_file = open("{0}.txt".format(message.author.id), "w")
    myfile1 = open("{0}.txt".format(message.author.id), "r")
    myfile1.read()
    my_file.write(myfile1.read() ++ number)
    my_file.close()

ERROR

Ignoring exception in on_message
Traceback (most recent call last):
  File "C:\Users\senuk\AppData\Local\Programs\Python\Python35\lib\site-
packages\discord\client.py", line 307, in _run_event
    yield from getattr(self, event)(*args, **kwargs)
  File "overmind.py", line 821, in on_message
    my_file.write(myfile1.read() ++ number)

number = 1

我试图读取消息的数量,然后将其显示在有关用户的统计信息中,但不明白错误是什么以及如何修复它,也许有人知道?

1 个答案:

答案 0 :(得分:0)

你错误地在同一个文件上混合读写。 基本上你想先读,然后写。您将使用递增的数字一次又一次地覆盖同一文件。

'0gpur'

你也可以打开r +并在阅读后截断文件 - 你需要用seek(0)跟踪截断,因为truncate对文件指针没有任何作用:If I truncate a file to zero in Python 3 do I also need to seek to position zero?

见: