RealStone
RealStone

Reputation: 13

Problems with pickle python

I recently made a program using an external document with pickle. But when it tries to load the file with pickle, I got this error (the file is already existing but it also fails when the file in't existing):

python3.6 check-graph_amazon.py

a
b
g
URL to follow www.amazon.com
Product to follow Pool_table
h
i
[' www.amazon.com', ' Pool_table', []]
p
Traceback (most recent call last):
  File "check-graph_amazon.py", line 17, in <module>
    tab_simple = pickle.load(doc_simple)
io.UnsupportedOperation: read

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "check-graph_amazon.py", line 42, in <module>
    pickle.dump(tab_simple, 'simple_data.dat')
TypeError: file must have a 'write' attribute

Here is the code :

import pickle5 as pickle
#import os

try:
    print("a")
    with open('simple_data.dat', 'rb') as doc_simple:
        print("b")
        tab_simple = pickle.load(doc_simple)
        print("c")
        print(tab_simple)
        print("d")
        URL = tab_simple[0]
        produit_nom = tab_simple[1]
        tous_jours = tab_simple[2]
        print("f")

except :
    print("g")
    URL = str(input("URL to follow"))
    produit_nom = str(input("Product to follow"))

    with open('simple_data.dat', 'wb+') as doc_simple:
        print("h")
        #os.system('chmod +x simple_data.dat') 
        tab_simple = []
        tab_simple.append(URL)
        tab_simple.append(produit_nom)
        tab_simple.append([])
        print(tab_simple)
        print("c'est le 2")
        print("p")
        pickle.dump(tab_simple, 'simple_data.dat')
        print("q")

The prints are here to show which lines are executed. The os.system is here to allow writing on the file but the error is persisting.

I don't understand why it's said that the document doesn't have a write attribute because I opened it in writing mode. And I neither understand the first error where it can't load the file.

If it can help you the goal of this script is to initialise the program, with a try. It tries to open the document in reading mode in the try part and then set variables. If the document doesn't exist (because the program is lauched for the first time) it goes in the except part and create the document, before writing informations on it.

I hope you will have any clue, including changing the architecture of the code if you have a better way to make an initialisation for the 1st time the program is launched.

Thanks you in advance and sorry if the code isn't well formated, I'm a beginner with this website.

Upvotes: 1

Views: 2489

Answers (1)

ForceBru
ForceBru

Reputation: 44828

Quote from the docs for pickle.dump:

pickle.dumps(obj, protocol=None, *, fix_imports=True)

Write a pickled representation of obj to the open file object file. ...

...

The file argument must have a write() method that accepts a single bytes argument. It can thus be an on-disk file opened for binary writing, an io.BytesIO instance, or any other custom object that meets this interface.

So, you should pass to this function a file object, not a file name, like this:

with open("simple_data.dat", "wb"): as File:
   pickle.dump(tab_simple, File)

Yeah, in your case the file has already been opened, so you should write to doc_simple.

Upvotes: 2

Related Questions