I've been trying to wrap my head around this problem for three days and can't figure it out.
I'm creating a disk usage analyzer that will create a weekly report about disk use. I have created a class that, given a parent directory, will automatically recurse through all sub-directories and create the information that I need. The class is called 'directory,' and contains a size attribute (size of all contained files/dirs), a full path name for the directory, and a list of 'directory' objects that are children to the current directory.
Basically I need to 'walk' through my tree of directories to sort by size and such. Currently, I'm trying to compile a short report on the top 10 directories by size. I need to walk through all the directories and return the path and size for each object.
The problem is that it's a weird mix of recursion and generation. A generator is very nice because I can do
That works great for a given directory's children, but I can't down to the next level, which is where recursion should come in. I thought that I could do
where _topDirs(directory) is the generator function. However, that doesn't work.
Can anyone help me walk through my tree and yield values along the way?
Relevant code:
Thanks,
I'm creating a disk usage analyzer that will create a weekly report about disk use. I have created a class that, given a parent directory, will automatically recurse through all sub-directories and create the information that I need. The class is called 'directory,' and contains a size attribute (size of all contained files/dirs), a full path name for the directory, and a list of 'directory' objects that are children to the current directory.
Basically I need to 'walk' through my tree of directories to sort by size and such. Currently, I'm trying to compile a short report on the top 10 directories by size. I need to walk through all the directories and return the path and size for each object.
The problem is that it's a weird mix of recursion and generation. A generator is very nice because I can do
Code:
for i in dir1.children:
yield i.path, i.size
Code:
for t in dir1.children:
yield _topDirs(t)
Can anyone help me walk through my tree and yield values along the way?
Relevant code:
Code:
class directory:
'''
Class that automatically traverses directories
and builds a tree with size info
'''
def __init__(self, path, parent=None):
if path[-1] != '/':
# Add trailing /
self.path = path + '/'
else:
self.path = path
self.size = 4096
self.parent = parent
self.children = []
self.errors = []
for i in os.listdir(self.path):
try:
self.size += os.lstat(self.path + i).st_size
if os.path.isdir(self.path + i) and not os.path.islink(self.path + i):
a = directory(self.path + i, self)
self.size += a.size
self.children.append(a)
except OSError:
self.errors.append(path + i)
Code:
def topDirs(dir1, N=5):
'''
Walks the tree and adds largest
directories to a returned list.
'''
if type(dir1) == str:
dir1 = directory(dir1)
large = []
for i in range(N):
large.append(('', 0))
for i, j in _topDirs(dir1):
if j > large[-1][1]:
large.append((i, j))
large = sorted(large, key=lambda t: t[1], reverse=True)
large.pop(-1)
return large
def _topDirs(dir1):
'''
Private function to walk the tree.
'''
for i in dir1.children:
yield i.path, i.size
for t in dir1.children:
yield _topDirs(t) # This doesn't work. How to recurse???
Thanks,