[Tutor] extracting a cPickle/pickle file from inside a zip file
diliup gabadamudalige
diliupg at gmail.com
Fri Aug 29 15:19:47 CEST 2014
http://stackoverflow.com/questions/3006727/load-a-pickle-file-from-a-zipfile
zf = czipfile.ZipFile(r"cpickletest.zip")
pyfromzip = zf.read ("cPickletest.py", pwd="python")
print pyfromzip, type(pyfromzip)
this prints the following to the console
(dp1
S'kola.jpg'
p2
S'CSVTQq#w. at lm'
p3
sS'p2.jpg'
p4
S'CSVT$S. at lm'
p5
sS'p1.jpg'
p6
S'CSVT3S. at lm'
p7
s. <type 'str'>
most probably this is the dictionary file written to disk as the keys and
values are all there.
now if i do this
outfile = cStringIO.StringIO()
outfile.write(pyfromzip)
z=cPickle.load(outfile)
it returns -> EOFError:
I tried various experiments and the code below worked.
filenames # this is a dictionary
with open(p + "\\coded\\" + name_py,"wb") as outfile:
cPickle.dump(str(filenames), outfile) # pickled dumped the
dictionary as a string
now the file is locked up in a pwd prtected zipfile using winrar
zf = czipfile.ZipFile(r"cpickletest.zip")
pyfromzip = zf.read ("cPickletest.py", pwd="password")
c = pyfromzip.partition('"')[-1].rpartition('"')[0]
c = eval(c)
print c, type(c) # this gave back the original dictionary
can someone think of a better way?
On Fri, Aug 29, 2014 at 6:25 PM, diliup gabadamudalige <diliupg at gmail.com>
wrote:
> Alan, thanks for the reply.
> nope. I tried both ways. Btw I am using Python 2.7.8.
> This seem to be a problem with python itself.
>
>
> http://stackoverflow.com/questions/3006727/load-a-pickle-file-from-a-zipfile
>
> zf = czipfile.ZipFile(r"cpickletest.zip")
> pyfromzip = zf.read ("cPickletest.py", pwd="python")
> print pyfromzip, type(pyfromzip)
>
> this prints the following to the console
>
> (dp1
> S'kola.jpg'
> p2
> S'CSVTQq#w. at lm'
> p3
> sS'p2.jpg'
> p4
> S'CSVT$S. at lm'
> p5
> sS'p1.jpg'
> p6
> S'CSVT3S. at lm'
> p7
> s. <type 'str'>
>
> most probably this is the dictionary file written to disk as the keys and
> values are all there.
>
> now if i do this
>
> outfile = cStringIO.StringIO()
> outfile.write(pyfromzip)
> z=cPickle.load(outfile)
> it returns -> EOFError:
>
>
>
> On Fri, Aug 29, 2014 at 5:01 PM, Alan Gauld <alan.gauld at btinternet.com>
> wrote:
>
>> On 29/08/14 10:00, diliup gabadamudalige wrote:
>>
>> pload = cPickle.load(zf.open("my.py",pwd=password))
>>> this gives an EOF
>>>
>>> . Does any body know why?
>>>
>>
>> I've never used zipfile so I don't know, but could it be
>> related to the fact that your pickle file is binary?
>> Do you need any flags to tell zipfile that you're
>> extracting a binary file?
>>
>> Just a thought,
>>
>> --
>> Alan G
>> Author of the Learn to Program web site
>> http://www.alan-g.me.uk/
>> http://www.flickr.com/photos/alangauldphotos
>>
>> _______________________________________________
>> Tutor maillist - Tutor at python.org
>> To unsubscribe or change subscription options:
>> https://mail.python.org/mailman/listinfo/tutor
>>
>
>
>
> --
> Diliup Gabadamudalige
>
> http://www.diliupg.com
> http://soft.diliupg.com/
>
>
> **********************************************************************************************
> This e-mail is confidential. It may also be legally privileged. If you are
> not the intended recipient or have received it in error, please delete it
> and all copies from your system and notify the sender immediately by return
> e-mail. Any unauthorized reading, reproducing, printing or further
> dissemination of this e-mail or its contents is strictly prohibited and may
> be unlawful. Internet communications cannot be guaranteed to be timely,
> secure, error or virus-free. The sender does not accept liability for any
> errors or omissions.
>
> **********************************************************************************************
>
>
--
Diliup Gabadamudalige
http://www.diliupg.com
http://soft.diliupg.com/
**********************************************************************************************
This e-mail is confidential. It may also be legally privileged. If you are
not the intended recipient or have received it in error, please delete it
and all copies from your system and notify the sender immediately by return
e-mail. Any unauthorized reading, reproducing, printing or further
dissemination of this e-mail or its contents is strictly prohibited and may
be unlawful. Internet communications cannot be guaranteed to be timely,
secure, error or virus-free. The sender does not accept liability for any
errors or omissions.
**********************************************************************************************
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20140829/a9053089/attachment-0001.html>
More information about the Tutor
mailing list