Mailing List Archive

Python-pickle error
Hello,
I am experincing problems with the pickle moducle
the folowing code was working before,

import pickle
number=2
my_pickeld_object=pickle.dumps(number)
print("this is my pickled object",{my_pickeld_object},)
with open('file.pkl', 'rb') as file:
number=pickle.load(file)
my_unpickeled_object=pickle.loads(my_pickeld_object)
print("this is my unpickeled object",{my_unpickeled_object},)

but now i get error

Traceback (most recent call last):
File "C:\Users\lukwi\Desktop\python\tester2.py", line 5, in <module>
with open('file.pkl', 'rb') as file:
FileNotFoundError: [Errno 2] No such file or directory: 'file.pkl'

im get this problem after this,
a .pkl came into my Python script files
i though this could be a spare file made from python becauce i was doing this first,

import pickle
number=2
my_pickeld_object=pickle.dumps(number)
print("this is my pickled object",{my_pickeld_object},)
with open('file.pkl', 'rb') as file:
number=pickle.load(file)

so i stupidly deleted the file

do you know how to fix this?
i reinstalled it but it didn't work
this is on widnows and on version 3.11.3 on python

thank you
--
https://mail.python.org/mailman/listinfo/python-list
Re: Python-pickle error [ In reply to ]
On 4/19/2023 12:14 PM, charles wiewiora wrote:
> Hello,
> I am experincing problems with the pickle moducle
> the folowing code was working before,
>
> import pickle
> number=2
> my_pickeld_object=pickle.dumps(number)
> print("this is my pickled object",{my_pickeld_object},)
> with open('file.pkl', 'rb') as file:
> number=pickle.load(file)
> my_unpickeled_object=pickle.loads(my_pickeld_object)
> print("this is my unpickeled object",{my_unpickeled_object},)
>
> but now i get error
>
> Traceback (most recent call last):
> File "C:\Users\lukwi\Desktop\python\tester2.py", line 5, in <module>
> with open('file.pkl', 'rb') as file:
> FileNotFoundError: [Errno 2] No such file or directory: 'file.pkl'

That's because you haven't saved anything to a file named "file.pkl". If
this code seemed to work in the past, it may have been because there was
a file named "file.pkl" left over from some previous experiment. But
it's not there now, and even if it were it would not contain your
current pickled object.

Take a look at .load() and .dump() (instead of .loads() and .dumps().
Maybe they will do what you want a little easier.

> im get this problem after this,
> a .pkl came into my Python script files
> i though this could be a spare file made from python becauce i was doing this first,
>
> import pickle
> number=2
> my_pickeld_object=pickle.dumps(number)
> print("this is my pickled object",{my_pickeld_object},)
> with open('file.pkl', 'rb') as file:
> number=pickle.load(file)
>
> so i stupidly deleted the file
>
> do you know how to fix this?
> i reinstalled it but it didn't work
> this is on widnows and on version 3.11.3 on python
>
> thank you

--
https://mail.python.org/mailman/listinfo/python-list
Re: Python-pickle error [ In reply to ]
Charles,

by your own admission, you deleted your pkl file,

And your code doesn't write that pkl file (pickle.dumps(...) doesn't
write a file it creates a new string and at no point will it write to
the file :

What you need is this :

import pickle
number=2
my_pickled_object=pickle.dumps(number)
with open('file.pkl', 'w') as file:
file.write(my_pickled_object)
print("this is my pickled object",{my_pickled_object},)

del number # you can do this if you really want to test pickle.

with open('file.pkl', 'r') as file:
number=pickle.load(file)

my_unpickled_object=pickle.loads(my_pickled_object)
print("this is my unpickled object",{my_unpickled_object},)

Note :  that the whole point of the pickle format is that you don't need
to open and write/read files in binary format.


On 19/04/2023 17:14, charles wiewiora wrote:
> Hello,
> I am experincing problems with the pickle moducle
> the folowing code was working before,
>
> import pickle
> number=2
> my_pickeld_object=pickle.dumps(number)
> print("this is my pickled object",{my_pickeld_object},)
> with open('file.pkl', 'rb') as file:
> number=pickle.load(file)
> my_unpickeled_object=pickle.loads(my_pickeld_object)
> print("this is my unpickeled object",{my_unpickeled_object},)
>
> but now i get error
>
> Traceback (most recent call last):
> File "C:\Users\lukwi\Desktop\python\tester2.py", line 5, in <module>
> with open('file.pkl', 'rb') as file:
> FileNotFoundError: [Errno 2] No such file or directory: 'file.pkl'
>
> im get this problem after this,
> a .pkl came into my Python script files
> i though this could be a spare file made from python becauce i was doing this first,
>
> import pickle
> number=2
> my_pickeld_object=pickle.dumps(number)
> print("this is my pickled object",{my_pickeld_object},)
> with open('file.pkl', 'rb') as file:
> number=pickle.load(file)
>
> so i stupidly deleted the file
>
> do you know how to fix this?
> i reinstalled it but it didn't work
> this is on widnows and on version 3.11.3 on python
>
> thank you

--
Anthony Flury
email : anthony.flury@btinternet.com

--
https://mail.python.org/mailman/listinfo/python-list