Python-pickle error

Thomas Passin list1 at tompassin.net
Wed Apr 19 17:12:04 EDT 2023


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



More information about the Python-list mailing list