79 root_path: (Path, str),
80 recursion_level: int = -1,
81 limit_to_directories: bool =
False,
82 length_limit: int = 1000,
86 if isinstance(root_path, str):
87 root_path = Path(root_path)
92 def is_junction(path: str) -> bool:
95 return bool(os.readlink(path))
105 nonlocal files, directories
109 if limit_to_directories:
110 objects = dir_path.iterdir()
111 contents = [d
for d
in objects
if d.is_dir()]
112 contents1 = list(objects)
114 contents = list(dir_path.iterdir())
118 for content
in contents1:
119 if content.is_file()
and (content.name ==
'.tree-excludes'):
120 with open(content,
'r')
as f:
121 excl1 = eval(f.read())
122 if content
not in contents:
123 contents.insert(0, content)
127 pointers = [tee] * (len(contents) - 1) + [last]
128 for pointer, path
in zip(pointers, contents):
133 if path.parts[-1] == item:
138 if excluded:
continue
142 (hasattr(path,
'is_junction')
and path.is_junction())
144 (is_junction(str(path)))
146 name =
'\x1b[105m {} \x1b[0m \x1b[95m→\x1b[0m \x1b[104m {} \x1b[0m'.format(
148 str(path.readlink()).split(
'\\\\?\\')[-1]
151 name =
'\x1b[104m {} \x1b[0m'.format(name)
152 yield prefix + pointer + name
154 extension = branch
if pointer == tee
else space
158 prefix=prefix+extension,
162 elif not limit_to_directories:
165 if path.is_symlink():
166 name1 =
'\x1b[95m● {}\x1b[0m'.format(name)
167 if name ==
'.python-version':
168 name1 +=
' \x1b[96m({})\x1b[0m'.format(
171 elif name ==
'.python-env':
172 name1 +=
' \x1b[93m({})\x1b[0m'.format(
175 elif name ==
'.tree-excludes':
176 name1 +=
' \x1b[95m{}\x1b[0m'.format(
180 name1 =
'\x1b[95m● {} → \x1b[37m● {}\x1b[0m'.format(
185 name1 =
'\x1b[37m● {}\x1b[0m'.format(name)
186 if name ==
'.python-version':
187 name1 +=
' \x1b[96m({})\x1b[0m'.format(
190 elif name ==
'.python-env':
191 name1 +=
' \x1b[93m({})\x1b[0m'.format(
194 elif name ==
'.tree-excludes':
195 name1 +=
' \x1b[95m{}\x1b[0m'.format(
199 name1 =
'\x1b[37m● {}\x1b[0m'.format(name)
200 yield prefix + pointer + name1
204 print(
'\x1b[104m {} \x1b[0m'.format(str(root_path)))
206 for excl_item
in exclude:
207 if root_path.parts[-1] == excl_item:
212 level=recursion_level,
216 for line
in islice(iterator, length_limit):
218 if next(iterator,
None):
220 print(
'... length_limit, {}, reached, counted:'.format(length_limit))
223 '\n{} directories'.format(directories) + (
224 ', {} files'.format(files)
if files
else ''
tree((Path, str) root_path, int recursion_level=-1, bool limit_to_directories=False, int length_limit=1000, tuple exclude=())
Given a directory path, print a visual tree structure.