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)
98 nonlocal files, directories
102 if limit_to_directories:
103 objects = dir_path.iterdir()
104 contents = [d
for d
in objects
if d.is_dir()]
105 contents1 = list(objects)
107 contents = list(dir_path.iterdir())
111 for content
in contents1:
112 if content.is_file()
and (content.name ==
'.tree-excludes'):
113 with open(content,
'r')
as f:
114 excl1 = eval(f.read())
115 if content
not in contents:
116 contents.insert(0, content)
120 pointers = [tee] * (len(contents) - 1) + [last]
121 for pointer, path
in zip(pointers, contents):
126 if path.parts[-1] == item:
131 if excluded:
continue
134 if path.is_junction():
135 name = f
'\x1b[105m {name} \x1b[0m \x1b[95m→\x1b[0m \x1b[104m {str(path.readlink()).split(f'\\\\?\\
')[-1]} \x1b[0m'
137 name = f
'\x1b[104m {name} \x1b[0m'
138 yield prefix + pointer + name
140 extension = branch
if pointer == tee
else space
144 prefix=prefix+extension,
148 elif not limit_to_directories:
151 if path.is_symlink():
152 name1 = f
'\x1b[95m● {name}\x1b[0m'
153 if name ==
'.python-version':
154 name1 += f
' \x1b[96m({
159 elif name ==
'.python-env':
160 name1 += f
' \x1b[93m({
165 elif name ==
'.tree-excludes':
166 name1 += f
' \x1b[95m{
172 name1 = f
'\x1b[95m● {name} → \x1b[37m● {str(path.readlink())}\x1b[0m'
174 name1 = f
'\x1b[37m● {name}\x1b[0m'
175 if name ==
'.python-version':
176 name1 += f
' \x1b[96m({
181 elif name ==
'.python-env':
182 name1 += f
' \x1b[93m({
187 elif name ==
'.tree-excludes':
188 name1 += f
' \x1b[95m{
194 name1 = f
'\x1b[37m● {name}\x1b[0m'
195 yield prefix + pointer + name1
199 print(f
'\x1b[104m {str(root_path)} \x1b[0m')
201 for excl_item
in exclude:
202 if root_path.parts[-1] == excl_item:
207 level=recursion_level,
211 for line
in islice(iterator, length_limit):
213 if next(iterator,
None):
215 print(f
'... length_limit, {length_limit}, reached, counted:')
218 f
'\n{directories} directories' + (
219 f
', {files} 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.