Python-pickle error

Tony Flury tony.flury at btinternet.com
Tue May 9 02:45:34 EDT 2023


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 at btinternet.com



More information about the Python-list mailing list