Search code examples
pythonpython-3.xfilemsgpack

"ValueError: Unpack failed: incomplete input"


with open('discord-gitlab.msgpack', 'rb') as database:
    try:
        print("Получены данные из файла БД:" + str(msgpack.unpackb(database.read())))
        discord_gitlab = msgpack.unpackb(database.read())
    finally:
        discord_gitlab = dict()
        print(msgpack.unpackb(database.read()))

Said about some "incomplete input" while there's no such mode in open().

Tried using close(), with and without with … as …:


Solution

  • print("Получены данные из файла БД:" + str(msgpack.unpackb(database.read())))
    discord_gitlab = msgpack.unpackb(database.read())
    

    The first line calls database.read(), which reads all of the data.

    Then the second line calls database.read() again, but there's nothing left to read, so read() returns an empty string.

    So msgpack.unpackb() receives an empty string, which presumably is the cause of the error.

    You can fix this by calling .read() only once and saving the results in a variable, like this:

    data = database.read()
    print("Получены данные из файла БД:" + str(msgpack.unpackb(data)))
    discord_gitlab = msgpack.unpackb(data)