Computing directory sizes problem

Neophytos Michael nmichael at yahoo.com
Fri Sep 6 14:35:14 EDT 2002


Dear Python experts,
I wrote the following function to compute the size of a directory
(being new to python there maybe better ways to do this - if so please
advice).  It works quite well except when the directory in question
contains files whose names that are not made up of ascii (english)
characters (this typically happens on my machine for my non-english
mp3 files).  For those files os.path.isfile returns false and so their
sizes are not added in the total sum.  All this is on win 2k.

Any help would be greatly appreciated.

Thank you,
Neophytos

--------------------------------------------------------------------
import os;
import os.path;

def dirSize(p):
   def visit(arg, dirname, names):
      paths = map(lambda x: os.path.join(dirname, x), names);
      files = filter(os.path.isfile, paths);
      arg[0] += reduce(lambda x,f: x + os.path.getsize(f), files, 0);

   siz = [0];
   os.path.walk(p, visit, siz);
   return siz[0];
--------------------------------------------------------------------



More information about the Python-list mailing list