[prev in list] [next in list] [prev in thread] [next in thread] 

List:       python-ideas
Subject:    [Python-ideas] Pickle to/from filename or path
From:       Todd <toddrjen () gmail ! com>
Date:       2020-02-07 18:03:52
Message-ID: CAFpSVp+UQQaNefCL2k8Cj_b6fV8UVyZ8yn69GcS65kmfzfbjKg () mail ! gmail ! com
[Download RAW message or body]

[Attachment #2 (multipart/alternative)]


Currently with pickle you have to "open" the file separately from the
pickle operation, generally using a "with" clause, then provide the file
object to one of the pickle.dump or pickle.load.  This adds quite a bit of
boilerplate for simply saving a file

I think it would be nice if pickle.dump and pickle.load would also accept a
filename or path.  The functions would automatically open the file in the
correct way, save/load the object, then close the file.  This would reduce
the amount of code needed to save a single object by at least half, and
would eliminate the incentive to use unsafe (I think?) approaches like
opening within the dump or load, which the wiki still recommends [1].

So something like:

with open('myfile.p', 'wb') as f:
    pickle.dump(myobj, f)

Would be:

pickle.dump(myobj, 'myfile.p')

Sorry if this has already been discussed, but I searched the mailing list
history and couldn't find it.

[1] https://wiki.python.org/moin/UsingPickle

[Attachment #5 (text/html)]

<div dir="ltr"><div>Currently with pickle you have to &quot;open&quot; the file separately from the \
pickle operation, generally using a &quot;with&quot; clause, then provide the file object to one of the \
pickle.dump or pickle.load.   This adds quite a bit of boilerplate for simply saving a \
file<br></div><div><br></div><div>I think it would be nice if pickle.dump and pickle.load would also \
accept a filename or path.   The functions would automatically open the file in the correct way, \
save/load the object, then close the file.   This would reduce the amount of code needed to save a single \
object by at least half, and would eliminate the incentive to use unsafe (I think?) approaches like \
opening within the dump or load, which the wiki still recommends [1].</div><div><br></div><div>So \
something like:</div><div><br></div><div>with open(&#39;myfile.p&#39;, &#39;wb&#39;) as f:</div><div>     \
pickle.dump(myobj, f)</div><div><br></div><div>Would be:</div><div><br></div><div>pickle.dump(myobj, \
&#39;myfile.p&#39;)</div><div><br></div><div>Sorry if this has already been discussed, but I searched the \
mailing list history and couldn&#39;t find it.<br></div><div><br></div><div>[1] <a \
href="https://wiki.python.org/moin/UsingPickle">https://wiki.python.org/moin/UsingPickle</a></div></div>



_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-leave@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at https://mail.python.org/archives/list/python-ideas@python.org/message/TSI4ETODZ7EKJUAUONPOSS3WZSXEUDNT/
Code of Conduct: http://python.org/psf/codeofconduct/


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic