C
Caleb Hattingh
Hi everyone
[Short version: I put a some code below: what changes can make it run
faster?]
Unless you have a nice tool handy, calculating many folder sizes for
clearing disk space can be a click-fest nightmare. Looking around, I
found Baobab (gui tool); the "du" linux/unix command-line tool; the
extremely impressive tkdu: http://unpythonic.net/jeff/tkdu/ ; a python
script I didn't really understand at
http://vsbabu.org/webdev/zopedev/foldersize.html (are these "folder
objects" zope thingies?); there are also tools that can add a
"foldersize" column into Explorer on Windows
(foldersize.sourceforge.net, for example); the superb freeCommander
file-manager (win32) has the functionality built in, and so on.
"du" is closest to what I was looking for, but is not immediately
cross-platform: I know I can probably get it through Cygwin, and there
is probably a win32 binary or clone around somewhere, but I thought a
simple python solution would be great. Maybe there already is one, but
I couldn't find it with a modest amount of searching.
Anyway, I made one that will produce a list of only the folders in the
current folder, along with their sizes. I am posting it for two
reasons: it might be useful for someone else, and I want to know if it
can be made faster (but in a cross-platform way); maybe you spot
something in the code that is obviously sub-optimal.
# Python script to list sizes of folders in current folder
import os, os.path
rootfolders = os.listdir('.')
rootfolders = [i for i in rootfolders if os.path.isdir(i)]
class counter:
def __init__(self,rootfolder):
self.count = 0
self.rootfolder = rootfolder
def inc(self,num):
self.count = self.count + num
def __str__(self):
if self.count<1024.:
unit = ' bytes'
scaler = 1.
elif self.count<1024.*1024.:
unit = ' KB'
scaler = 1/1024.
elif self.count<1024.*1024.*1024.:
unit = ' MB'
scaler = 1/1024./1024.
else:
unit = ' GB'
scaler = 1/1024./1024./1024.
return '%-20s -
%8.2f%s'%(self.rootfolder,self.count*scaler,unit)
def visitfun(cntObj,dirname,names):
for i in names:
fullname = os.path.join(dirname,i)
if os.path.isfile(fullname):
cntObj.inc( os.path.getsize(fullname) )
return None
foldersizeobjects = []
for i in rootfolders:
cntObj = counter(i)
os.path.walk(i,visitfun,cntObj)
foldersizeobjects.append(cntObj)
def cmpfunc(a,b):
if a.count > b.count:
return 1
elif a.count == b.count:
return 0
else:
return -1
foldersizeobjects.sort(cmpfunc)
tot=0
for foldersize in foldersizeobjects:
tot=tot+foldersize.count
print foldersize
print 'Total: %.2f MB'%(tot/1024./1024.)
# End
regards
Caleb
[Short version: I put a some code below: what changes can make it run
faster?]
Unless you have a nice tool handy, calculating many folder sizes for
clearing disk space can be a click-fest nightmare. Looking around, I
found Baobab (gui tool); the "du" linux/unix command-line tool; the
extremely impressive tkdu: http://unpythonic.net/jeff/tkdu/ ; a python
script I didn't really understand at
http://vsbabu.org/webdev/zopedev/foldersize.html (are these "folder
objects" zope thingies?); there are also tools that can add a
"foldersize" column into Explorer on Windows
(foldersize.sourceforge.net, for example); the superb freeCommander
file-manager (win32) has the functionality built in, and so on.
"du" is closest to what I was looking for, but is not immediately
cross-platform: I know I can probably get it through Cygwin, and there
is probably a win32 binary or clone around somewhere, but I thought a
simple python solution would be great. Maybe there already is one, but
I couldn't find it with a modest amount of searching.
Anyway, I made one that will produce a list of only the folders in the
current folder, along with their sizes. I am posting it for two
reasons: it might be useful for someone else, and I want to know if it
can be made faster (but in a cross-platform way); maybe you spot
something in the code that is obviously sub-optimal.
# Python script to list sizes of folders in current folder
import os, os.path
rootfolders = os.listdir('.')
rootfolders = [i for i in rootfolders if os.path.isdir(i)]
class counter:
def __init__(self,rootfolder):
self.count = 0
self.rootfolder = rootfolder
def inc(self,num):
self.count = self.count + num
def __str__(self):
if self.count<1024.:
unit = ' bytes'
scaler = 1.
elif self.count<1024.*1024.:
unit = ' KB'
scaler = 1/1024.
elif self.count<1024.*1024.*1024.:
unit = ' MB'
scaler = 1/1024./1024.
else:
unit = ' GB'
scaler = 1/1024./1024./1024.
return '%-20s -
%8.2f%s'%(self.rootfolder,self.count*scaler,unit)
def visitfun(cntObj,dirname,names):
for i in names:
fullname = os.path.join(dirname,i)
if os.path.isfile(fullname):
cntObj.inc( os.path.getsize(fullname) )
return None
foldersizeobjects = []
for i in rootfolders:
cntObj = counter(i)
os.path.walk(i,visitfun,cntObj)
foldersizeobjects.append(cntObj)
def cmpfunc(a,b):
if a.count > b.count:
return 1
elif a.count == b.count:
return 0
else:
return -1
foldersizeobjects.sort(cmpfunc)
tot=0
for foldersize in foldersizeobjects:
tot=tot+foldersize.count
print foldersize
print 'Total: %.2f MB'%(tot/1024./1024.)
# End
regards
Caleb