Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
pickle error
#1
I am making a shortcut thing just for fun and I'm using pickle to store the shortcuts. The problem is the error I'm getting. TIA for your help

error -
Error:
Traceback (most recent call last): File "C:\Users\Chadd\Desktop\Shortcuts\Url Shortcuts.py", line 63, in <module> pickleGrab() File "C:\Users\Chadd\Desktop\Shortcuts\Url Shortcuts.py", line 43, in pickleGrab shortcutDict = pickle.loads(file) TypeError: a bytes-like object is required, not '_io.BufferedReader'
Url Shortcuts.py -
import webbrowser
import time
import pickle
import os
from pathlib import Path

shortcutDict = {}
d = Path(__file__).parent

def addShortcut():
    global shortcutDict
    url = input("What is the url ")
    name = input("What would you like to name this shortcut ")
    shortcutDict.update({name : url})
    pickleDump()
    startUp()

def chooseShortcut():
    global shortcutDict
    print("These are your shortcuts")
    print('')
    for i in shortcutDict:
        print(" - " + i)
    name = input("Which shortcut would you like to use ")
    if name in shortcutDict:
        webbrowser.open(shortcutDict[name])
        startUp()
    else:
        print("Shortcut not found. Please browse the list of shortcuts or create a new one")
        startUp()

def pickleDump():
    global shortcutDict
    file = open(os.path.join(str(d), 'shortcutData.txt'), 'wb')
    pickle.dump(shortcutDict, file)
    file.close()

def pickleGrab():
    global shortcutDict
    file = open(os.path.join(str(d), 'shortcutData.txt'), 'rb')
    print(os.path.join(str(d), 'shortcutData.txt'))
    print(file)
    shortcutDict = pickle.loads(file)
    print(shortcutDict)

def startUp():
    global shortcutDict
    decide = input("Would you like to add a shortuct y/n ")

    if decide == 'y':
        if shortcutDict == {}:
            print("You have no shortcuts - you must add one")
            startUp
        else:
            addShortcut()
    elif decide == 'n':
        chooseShortcut()

    else:
        print("You said %s, please try again" %decide)
        startUp()

pickleGrab()
startUp()
Reply
#2
Use pickle.load() instead of pickle.loads()
Reply
#3
I tried that but it says -
Error:
Traceback (most recent call last): File "C:\Users\Chadd\Desktop\Shortcuts\Url Shortcuts.py", line 63, in <module> pickleGrab() File "C:\Users\Chadd\Desktop\Shortcuts\Url Shortcuts.py", line 43, in pickleGrab shortcutDict = pickle.load(file) _pickle.UnpicklingError: pickle data was truncated
Reply
#4
Then the file looks corrupted, try to recreate it.
Reply
#5
I'll try that
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  TypeError: cannot pickle ‘_asyncio.Future’ object Abdul_Rafey 1 273 Mar-07-2024, 03:40 PM
Last Post: deanhystad
  pickle problem DPaul 13 4,541 Sep-27-2022, 05:25 PM
Last Post: DPaul
  TypeError: cannot pickle n00sferatu 1 2,607 Dec-14-2021, 03:52 PM
Last Post: yakkaligiri
  Multiprocessing Can't pickle local object law 1 15,721 Aug-30-2021, 02:49 PM
Last Post: law
  Save/Loading using pickle Scordomaniac 4 2,977 Nov-24-2020, 06:11 PM
Last Post: Scordomaniac
  computing entropy using pickle files baran01 2 2,374 Dec-30-2019, 09:45 PM
Last Post: micseydel
  Tkinter don't get ver from file via pickle storzo 2 2,520 Jul-31-2019, 03:50 PM
Last Post: storzo
  pickle docs say bytes in one place, strings in another Skaperen 2 2,107 Jul-29-2019, 05:13 PM
Last Post: Skaperen
  Using pickle.dump Friend 1 2,901 Feb-15-2019, 04:39 PM
Last Post: metulburr
  I'm having trouble with an OOP version of Pickle functionality CodeWolf 2 2,322 Dec-19-2018, 05:41 PM
Last Post: CodeWolf

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020