Last active
January 29, 2026 06:27
-
-
Save kouichi-c-nakamura/dc741e1d3ae45b04b5acfe120fdfa46b to your computer and use it in GitHub Desktop.
Modified from https://stackoverflow.com/questions/9727673/list-directory-tree-structure-in-python This version supports suppression of file names, just printing folder structure by option
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Source - https://stackoverflow.com/a/59109706 | |
| # Posted by Aaron Hall, modified by community. | |
| # Modified by Assistant to include dirs_only, limit_to_depth, and exclude_hidden options. | |
| # Retrieved 2026-01-26, License - CC BY-SA 4.0 | |
| from pathlib import Path | |
| # prefix components: | |
| space = ' ' | |
| branch = '│ ' | |
| # pointers: | |
| tee = '├── ' | |
| last = '└── ' | |
| def tree(dir_path: Path, prefix: str='', dirs_only: bool=False, limit_to_depth: int=None, exclude_hidden: bool=False): | |
| """A recursive generator, given a directory Path object | |
| will yield a visual tree structure line by line. | |
| Args: | |
| dir_path (Path): The path to visualize. | |
| prefix (str): Current line prefix (used for recursion). | |
| dirs_only (bool): If True, only directories are shown. | |
| limit_to_depth (int): Max depth to traverse (0 for top level). None for no limit. | |
| exclude_hidden (bool): If True, files/dirs starting with '.' are ignored. | |
| """ | |
| try: | |
| contents = list(dir_path.iterdir()) | |
| except PermissionError: | |
| return # 権限がないフォルダはスキップ | |
| # 1. 不可視ファイル/フォルダを除外 (名前が '.' で始まるもの) | |
| if exclude_hidden: | |
| contents = [p for p in contents if not p.name.startswith('.')] | |
| # 2. ディレクトリのみの場合 | |
| if dirs_only: | |
| contents = [p for p in contents if p.is_dir()] | |
| # 3. 見やすくするためにソートする (Windows/Linuxで挙動を統一) | |
| contents.sort(key=lambda x: x.name.lower()) | |
| # 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(): | |
| # Check depth limit before recursing | |
| if limit_to_depth is not None and limit_to_depth <= 0: | |
| continue | |
| extension = branch if pointer == tee else space | |
| # Next depth limit | |
| next_limit = limit_to_depth - 1 if limit_to_depth is not None else None | |
| # 再帰呼び出し時にも exclude_hidden を引き継ぐ | |
| yield from tree(path, prefix=prefix+extension, dirs_only=dirs_only, | |
| limit_to_depth=next_limit, exclude_hidden=exclude_hidden) | |
| if __name__ == '__main__': | |
| # 使用例: 現在のディレクトリ | |
| target_path = Path('.') | |
| # Example: 隠しファイルを無視する (exclude_hidden=True) | |
| print(f"--- Tree for {target_path.resolve().name} (No Hidden Files) ---") | |
| # ここで exclude_hidden=True を指定します | |
| for line in tree(target_path, exclude_hidden=True): | |
| print(line) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment