zipfile模块的内存错误 [英] memory error with zipfile module

查看:99
本文介绍了zipfile模块的内存错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我这样做


导入zipfile

zip = zipfile.ZipFile(''d:\ somepath \ cfdimage.zip'')

zip.namelist()

[''someimage.iso'']


然后两者中的任何一个:


A)file(''someimage.iso'',''w'')。write(zip.read(''someimag e.iso''))



B)content = zip.read(''someimage.iso'')


但两者都会导致同样的错误:


回溯(最近一次调用最后一次):

文件"< stdin>",第1行,在?

文件" D:\\\\Python24 \lib \ zipfile.py",第357行,读取

bytes = dc.decompress(bytes)

MemoryError


我以为python应该为你处理内存?


python zipfile模块显然已经坏了...

有任何建议吗?

解决方案

Hari Sekhon写道:

我做

导入zipfile
zip = zipfile.ZipFile(''d:\ somepath \ _cdimage.zip'')
zip.namelist()
[''someimage.iso'']

接下来的两个:

A)文件(''someimage.iso'',''w' ')。write(zip.read(''someimag e.iso''))

B)content = zip.read(''someimage.iso'')

Traceback(最近一次调用最后一次):
文件"< stdin>",第1行,在?
文件中D:\\\\Python24 \lib \ zipfile.py,第357行,读取
bytes = dc.decompress(bytes)
MemoryError


< ot>那是* full * traceback?< / ot>

我以为python应该为你处理内存?


错误...这并不意味着它绕过系统的内存管理。

http://pyref.infogami.com/MemoryError

http://mail.zope.org/pipermail/zope/...er/153882。 html

"""

当底层(操作系统级别)分配

失败时,Python会引发MemoryError 。

(...)

通常情况下这意味着你甚至没有虚拟内存

(交换),但也可能是一个libc bug,一个坏的RAM芯片等的症状。

"""


如果你尝试,你认为会有什么附加?当你已经吃掉所有可用的内存时,分配一个巨大的块?你真的希望

Python会免费给你额外的ram吗? - )


请试试这个代码:


import zipfile

zip = zipfile.ZipFile(''d:\ somepath \ _cdimage.zip'')

info = zip.getinfo('' someimage.iso'')

csize = info.compress_size

fsize = info.file_size

print" someimage压缩大小为:%s" ; %csize

print" someimage真实文件大小为:%s" %fsize

print"""

所以,知道zipfile.read()实际是如何实现的,

总需要ram是:%s

""" %(csize + fsize)


print"嗯......我有那么多可用的内存吗???"


python zipfile模块显然已被破坏...


s /显然已被破坏/可以改进以处理大文件/


发表此类声明可能不是结交朋友的最佳方式......

有什么建议吗?



是的:Python是免费软件(免费',如''免费演讲''*和*在

''免费啤酒''),主要由仁慈的贡献者撰写。所以尝试和

自己改进zipfile模块,并提交你的增强功能。

然后我们都将非常感激,你的名字将永远在

Python名人堂。


或者选择表现得像一个发牢骚的无知的luser制作愚蠢的

语句,你的名字将是很长一段时间埋葬很多

的killfile。


这取决于你! -


注意:如果你走第一条路线,这可能会有所帮助:
http://www.python.org/doc/2.4.2/lib/module-zlib.html

特别注意解压缩。


HTH

-

bruno desthuilliers

python -c" print ''@''。join([''。''。join([w [:: - 1] for p in p.split(''。'')])for

p in ''o **** @ xiludom.gro''。split(''''')])"

看一下pywin32扩展,我相信它有一些较低的内存分配和文件功能可能会帮助你在这种情况下获得
。如果我完全错了,有人请告诉我XD。

当然,你可以让read()迈出一步,阅读,O

让我们说一次8192个字节(如果你想要的话可​​能更大),将它们写入新文件,然后读取下一部分。如果你有一台AMD X2 64和3台ram

并且可以一次读取所有文件,那么这将会更慢(不确定多少)。它应该工作。


jo ************ @ gmail.com 写道:

看看pywin32扩展,我相信它有一些较低级别的内存分配和文件功能可能会帮助你解决这种情况。




但是解决方案不会是便携式的,这将是一种耻辱
$ z $ b因为zlib模块(ZipFile依赖于压缩/
解压缩)已经拥有处理流所需的一切。


-

bruno desthuilliers

python -c" print''@''。join([''。''。join([w [:: - 1] for w in p.split(''。'')])

p in''o **** @ xiludom.gro''。split(''''')])"

I do

import zipfile
zip=zipfile.ZipFile(''d:\somepath\cdimage.zip'')
zip.namelist()
[''someimage.iso'']

then either of the two:

A) file(''someimage.iso'',''w'').write(zip.read(''someimag e.iso''))
or
B) content=zip.read(''someimage.iso'')

but both result in the same error:

Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "D:\u\Python24\lib\zipfile.py", line 357, in read
bytes = dc.decompress(bytes)
MemoryError

I thought python was supposed to handle memory for you?

The python zipfile module is obviously broken...
Any advise?

解决方案

Hari Sekhon wrote:

I do

import zipfile
zip=zipfile.ZipFile(''d:\somepath\cdimage.zip'')
zip.namelist()
[''someimage.iso'']

then either of the two:

A) file(''someimage.iso'',''w'').write(zip.read(''someimag e.iso''))
or
B) content=zip.read(''someimage.iso'')

but both result in the same error:

Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "D:\u\Python24\lib\zipfile.py", line 357, in read
bytes = dc.decompress(bytes)
MemoryError
<ot>Is that the *full* traceback ?</ot>
I thought python was supposed to handle memory for you?
Err... This doesn''t mean that it bypasses system''s memory management.

http://pyref.infogami.com/MemoryError

http://mail.zope.org/pipermail/zope/...er/153882.html
"""
MemoryError is raised by Python when an underlying (OS-level) allocation
fails.
(...)
Normally this would mean that you were out of even virtual memory
(swap), but it could also be a symptom of a libc bug, a bad RAM chip, etc.
"""

What do you think will append if you try to allocate a huge block when
you''ve already eaten all available memory ? Do you really hope that
Python will give you extra ram for free ?-)

Please try this code:

import zipfile
zip=zipfile.ZipFile(''d:\somepath\cdimage.zip'')
info = zip.getinfo(''someimage.iso'')
csize = info.compress_size
fsize = info.file_size
print "someimage compressed size is : %s" % csize
print "someimage real file size is : %s" % fsize
print """
So, knowing how zipfile.read() is actually implemented,
total needed ram is : %s
""" % (csize + fsize)

print "Well... Do I have that much memory available ???"

The python zipfile module is obviously broken...
s/is obviously broken/could be improved to handle huge files/

Making such statements may not be the best way to make friends...
Any advise?


Yes : Python is free software (''free'' as in ''free speach'' *and* as in
''free beer''), mostly written by benevolent contributors. So try and
improve the zipfile module by yourself, and submit your enhancements.
Then we all will be very grateful, and your name will be forever in the
Python Hall of Fame.

Or choose to behave as a whiny-whiny clueless luser making dumb
statements, and your name will be buried for a long long time in a lot
of killfiles.

It''s up to you !-)

NB : If you go the first route, this may help:
http://www.python.org/doc/2.4.2/lib/module-zlib.html
with particular attention to the decompressobj.

HTH
--
bruno desthuilliers
python -c "print ''@''.join([''.''.join([w[::-1] for w in p.split(''.'')]) for
p in ''o****@xiludom.gro''.split(''@'')])"


Take a look at the pywin32 extension, which I believe has some lower
level memory allocation and file capabilities that might help you in
this situation. If I''m completely wrong, someone please tell me XD.
Of course, you could just make the read() a step process, reading, O
lets say 8192 bytes at a time (could be bigger if u want), writes them
to the new file, and then reads the next portion. This will be slower
(not sure how much) than if you had some AMD X2 64 with 3 gigs of ram
and could just read the file all at once, but it should work.


jo************@gmail.com wrote:

Take a look at the pywin32 extension, which I believe has some lower
level memory allocation and file capabilities that might help you in
this situation.



But then the solution would not be portable, which would be a shame
since the zlib module (on which ZipFile relies for compression /
decompression) already has everything needed to handle streams.

--
bruno desthuilliers
python -c "print ''@''.join([''.''.join([w[::-1] for w in p.split(''.'')]) for
p in ''o****@xiludom.gro''.split(''@'')])"


这篇关于zipfile模块的内存错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆