blog/
Вывод дерева файлов через Питон
Взято тут, добавил r к строке пути, чтобы работало.
from pathlib import Path
# prefix components:
space = ' '
branch = '│ '
# pointers:
tee = '├── '
last = '└── '
def tree(dir_path: Path, prefix: str=''):
"""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)
for line in tree(Path.home() / r'O:\Proekty\_материалы'):
print(line)
Similar Posts:
- Как скачать картинки с сайта с помощью Python
- Select all images urls and download them
- Script for choosing a winner on Instagram (JavaScript Instagram comment picker)
- Про Python и ArchiCAD: удалить лишние слои
- Распечатать дерево проекта в ArchiCAD 23 через Python
▲