discord.ext.commands.errors.CommandInvokeError:命令引发了异常:UnpicklingError:无效的加载键'H'

时间:2018-10-01 01:09:55

标签: python pickle discord.py jsonpickle

我已经在Stackoverflow上进行了全部搜索,但找不到修复程序。授予许多其他人同样的错误,当我查找它们时它们并没有真正帮助您。错误是:

  

CommandInvokeError:命令引发了异常:UnpicklingError:无效的加载键'H'

这是我目前的代码:

@bot.command(pass_context=True)
async def joke(ctx):
    with open("joke_file1.pk1", "rb") as f:
            joke_list = pickle.load(f)
    await bot.say(random.choice(joke_list))

@bot.command(pass_context=True)
async def addjoke(ctx, *args):
    if not os.path.isfile("joke_file1.pk1"):
            joke_list = []
    else:
        with open("joke_file1.pk1", "rb") as f:
            joke_list = pickle.load(f)
    joke_list.append(" ".join(args))
    with open("joke_file1.pk1", "wb") as f:
        pickle.dump(joke_list, f)

那是在我的主文件中,我还有另一个文件,可让其从我拥有的.txt文件中读取,这是该文件的代码

import pickle

with open("joke_file1.pk1", "rb") as f:
    jokes = pickle.load(f)
with open("joke_title1.txt", "r") as f:
    data = f.readlines()
for joke in data:
    jokes.append(joke.replace("\n", ""))
with open("joke_file1.pk1", "wb") as f:
    pickle.dump(jokes, f)

在第二个文件上,我得到与上一个相同的错误。如果有人可以帮助我解决此问题,谢谢!!

此图像是所有文件的外观。 Cow.py是读取.txt文件的代码 This image is what all the files look like. Cow.py is the code that reads the .txt file

1 个答案:

答案 0 :(得分:0)

我固定了一切。我要做的就是删除预设文件,然后再次运行Bot。一旦运行,一切都会恢复。 T