MicroStation Python: List Directories

  • All Publications
  • Links
  • LA Solutions
  • Home
LA Solutions
tel: +44 1398 361 800

Python

Introduction

We're all familiar with computer system folders, files and directories. As a developer, I occasionally want to show the folder structure of a Python app. That makes it easier for the reader to comprehend my Python modules and what they contain.

List Directories

A web search for Python list directories revealed several possibilities. I settled on this one, which I found on Stack Overflow.

Implementation

I made a couple of minor changes to ensure that the code worked with MicroStation Python. Here's the code …

# -*- coding: utf-8 -*-

from pathlib import Path
from itertools import islice

# Source - https://stackoverflow.com/a/59109706
# Posted by Aaron Hall, modified by community. See post 'Timeline' for change history
# https://stackoverflow.com/questions/9727673/list-directory-tree-structure-in-python
# Retrieved 2026-03-18, License - CC BY-SA 4.0

# prefix components.  These are Unicode characters:
SPACE =  '    '
BRANCH = '│   '
# pointers:
TEE =    '├── '
LAST =   '└── '

def tree(dir_path: Path, level: int=-1, limit_to_directories: bool=False,
         length_limit: int=1000):
    """ Given a directory Path object print a visual tree structure

        See: https://stackoverflow.com/questions/9727673/list-directory-tree-structure-in-python
    """
    dir_path = Path(dir_path) # accept string coerceable to Path
    files = 0
    directories = 0
    def inner(dir_path: Path, prefix: str='', level=-1):
        nonlocal files, directories
        if not level:
            return # 0, stop iterating
        if limit_to_directories:
            contents = [d for d in dir_path.iterdir() if d.is_dir()]
        else:
            contents = list(dir_path.iterdir())
        pointers = [TEE] * (len(contents) - 1) + [LAST]
        for pointer, path in zip(pointers, contents):
            if path.is_dir():
                yield prefix + pointer + path.name
                directories += 1
                extension = BRANCH if pointer == TEE else SPACE
                yield from inner(path, prefix=prefix+extension, level=level-1)
            elif not limit_to_directories:
                yield prefix + pointer + path.name
                files += 1
    print(dir_path.name)
    iterator = inner(dir_path, level=level)
    for line in islice(iterator, length_limit):
        print(line)
    if next(iterator, None):
        print(f'... length_limit, {length_limit}, reached, counted:')
    print(f'\n{directories} directories' + (f', {files} files' if files else ''))

if __name__ == "__main__":
    DIRECTORY_FULL_PATH = 'Path to your Python folder'
    tree(Path(DIRECTORY_FULL_PATH))

There are no MicroStation dependencies. The code will run with any Python interpreter inside or outside MicroStation.

Run this code from the Python command window. The result should look something like this …

├── package
│   ├── __init__.py
│   ├── __main__.py
│   ├── subpackage
│   │   ├── __init__.py
│   │   ├── __main__.py
│   │   └── module.py
│   └── subpackage2
│       ├── __init__.py
│       ├── __main__.py
│       └── module2.py
└── package2
    └── __init__.py

Unicode

When I started working on this project I was using MicroStation 2025. The Unicode characters (SPACE, BRANCH, TEE, LAST) did not display correctly. Fortunately, with MicroStation 2026 they do display correctly.

Thanks to Robert Hook for providing assistance on the MicroStation Programming Forum!


Trademarks

All trademarks are acknowledged. Trademarks may be registered in some jurisdictions.

Home
Home
Updated on 19-Mar-2026 Copyright © 2026…2026 Jon Summers