<filename>terraform_init/tools.py<gh_stars>0
from pathlib import Path


def ptree(dir_path):
    print(dir_path)
    p = Path(dir_path)
    if p.is_dir():
        for line in tree(p):
            print(line)


def tree(dir_path: Path, prefix: str = ""):
    # prefix components:
    space = "    "
    branch = "│   "
    # pointers:
    tee = "├── "
    last = "└── "
    """A recursive generator, given a directory Path object
    will yield a visual tree structure line by line
    with each line prefixed by the same characters
    """
    contents = list(dir_path.iterdir())
    # contents each get pointers that are ├── with a final └── :
    pointers = [tee] * (len(contents) - 1) + [last]
    for pointer, path in zip(pointers, contents):
        yield prefix + pointer + path.name
        if path.is_dir():  # extend the prefix and recurse:
            extension = branch if pointer == tee else space
            # i.e. space because last, └── , above so no more |
            yield from tree(path, prefix=prefix + extension)

<filename>terraform_init/tools.py<gh_stars>0
from pathlib import Path


def ptree(dir_path):
    print(dir_path)
    p = Path(dir_path)
    if p.is_dir():
        for line in tree(p):
            print(line)


def tree(dir_path: Path, prefix: str = ""):
    # prefix components:
    space = "    "
    branch = "│   "
    # pointers:
    tee = "├── "
    last = "└── "
    """A recursive generator, given a directory Path object
    will yield a visual tree structure line by line
    with each line prefixed by the same characters
    """
    contents = list(dir_path.iterdir())
    # contents each get pointers that are ├── with a final └── :
    pointers = [tee] * (len(contents) - 1) + [last]
    for pointer, path in zip(pointers, contents):
        yield prefix + pointer + path.name
        if path.is_dir():  # extend the prefix and recurse:
            extension = branch if pointer == tee else space
            # i.e. space because last, └── , above so no more |
            yield from tree(path, prefix=prefix + extension)

# prefix components: # pointers: A recursive generator, given a directory Path object     will yield a visual tree structure line by line     with each line prefixed by the same characters # contents each get pointers that are ├── with a final └── : # extend the prefix and recurse: # i.e. space because last, └── , above so no more |