My goal is to store all files and directories in a structured data tree, where each:
- directory is a node
- file is a leaf
My code below works fine. However, I only take one step at a time and interrupt/restart the walking process for every directory. (see step_in()
method)
Apparently it is possible and considered "advanced" to break into the process of an iteration itself and work with it. Therefore my question is, is it possible to "break into" the os.walk process and yield what's necessary?
import os
import sys
import inspect
DEBUG = True
def report(*args,**kwargs):
global DEBUG
if DEBUG: print(*args,**kwargs)
class directory:
def __init__(self, path):
self.path = path
@property
def name(self):
return os.path.basename(self.path)
def __repr__(self):
ID = hex(id(self))
return "<directory \"{:}\" at {}>".format(self.name,ID)
def step_in(self):
"""Step into the dir and find all files/dirs.
Step into the directory path and search for:
- directories --> add string name to children (SEMI CHILD)
- and files --> add string name to leafs
"""
for p,d,f in os.walk(self.path):
self.children = d
report("--->kids found : {}".format(d))
self.leafs = f
report("--->leafs found: {}".format(f))
return p
class walker:
def __init__(self, root_path):
self.root = directory(root_path)
def walk(self, target=None):
"""Walk through all dirs and create tree.
Recursive process with root directory as initial directory.
"""
if not(target):
target = self.root
path = target.step_in()
for i in range(len(target.children)):
#get the next path
next_path = os.path.join(path,target.children[i])
report("\nnext is: {}".format(next_path))
#save dir by replacing the string child with an actual child
target.children[i] = directory(next_path)
#walk into that child
self.walk(target.children[i])
if __name__ == "__main__":
w = walker('/Users/xxx/test/xxx')
w.walk()