[Tutor] If you don't close file when writing, do bytes stay in memory?

xbmuncher xboxmuncher at gmail.com
Sat Oct 10 06:20:45 CEST 2009


Which piece of code will conserve more memory?
 I think that code #2 will because I close the file more often, thus freeing
more memory by closing it.
Am I right in this thinking... or does it not save me any more bytes in
memory by closing the file often?
Sure I realize that in my example it doesn't save much if it does... but I'm
dealing with writing large files.. so every byte freed in memory counts.
Thanks.

CODE #1:
def getData(): return '12345' #5 bytes
f = open('file.ext', 'wb')
for i in range(2000):
    f.write(getData())

f.close()


CODE #2:
def getData(): return '12345' #5 bytes
f = open('file.ext', 'wb')
for i in range(2000):
    f.write(getData())
    if i == 5:
        f.close()
        f = open('file.ext', 'ab')
        i = 1
    i = i + 1

f.close()
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091010/ad41b06d/attachment.htm>


More information about the Tutor mailing list