memory error

fishboy fishboy at
Wed Jun 2 17:48:51 CEST 2004

On Wed, 02 Jun 2004 09:11:13 -0400, Bart Nessux
<bart_nessux at> wrote:

>def windows():
>    import os
>    excludes = ['hiberfil.sys', 'ipnathlp.dll', 'helpctr.exe', 'etc', 
>'etc', 'etc']
>    size_list = []
>    for root, dirs, files in os.walk('/'):
>       total = [x for x in files if x not in excludes]
>       for t in total:
>          s = file(os.path.join(root,t))
>          size =
>          size_list.append(size)
>          s.close()

Yeah, what the other guys said about os.stat and os.path.getsize.
Also, if you really want to read the actual file into memory, just get
small chunks and add those up.

Like (untested):

numberofbytes = 0
for root,dirs, files in os.walk('/'):
    for name in files:
        if name not in excludes:
	f = file(os.path.join(root,name))
	while 1:
	    s =
	    if not s:
	    numberofbytes += len(s)

this way you never have more than 4k of data in memory at once.
(well it might be 8k, I dont know enought about the internals to tell
you when the previous 's' is garbage collected.)


More information about the Python-list mailing list