0

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()
NewNewton
  • 1,015
  • 1
  • 10
  • 22
  • I don't know what you mean by "break into the os.walk process and yield what's necessary". As you've noted, you aren't really walking the directory tree, you could just use `os.scandir`... it sseems to me you should just be using `os.walk` in your `walk` method... not sure why you are using it in `step_in`, since `step_in` just adds directorys as children and files as leafs... – juanpa.arrivillaga Feb 27 '18 at 20:37
  • Yeah, exactly that is the point :/ I could use `os.scandir` instead, but want to implement it in a way, where I actually make use of `os.walk` – NewNewton Feb 27 '18 at 23:32
  • I struggle using 'os.walk` in my walk method.. – NewNewton Feb 27 '18 at 23:32

0 Answers0