I know that we can use os.walk() to list all sub-directories or all files in a directory. However, I would like to list the full directory tree content:
- Subdirectory 1:
- file11
- file12
- Sub-sub-directory 11:
- file111
- file112
- Subdirectory 2:
- file21
- sub-sub-directory 21
- sub-sub-directory 22
- sub-sub-sub-directory 221
- file 2211
- sub-sub-sub-directory 221
How to best achieve this in Python?
Based on this fantastic post
http://code.activestate.com/recipes/217212-treepy-graphically-displays-the-directory-structur/
Here es a refinement to behave exactly like
http://linux.die.net/man/1/tree
You can execute 'tree' command of Linux shell.
Installation:
Using in python
Example:
This gives you a cleaner structure and is visually more comprehensive and easy to type.
Maybe faster than @ellockie ( Maybe )
Test results in screenshot below:
Here's a function to do that with formatting:
A solution without your indentation:
os.walk already does the top-down, depth-first walk you are looking for.
Ignoring the dirs list prevents the overlapping you mention.
Similar to answers above, but for python3, arguably readable and arguably extensible:
Example usage:
Example output:
Notes
Edit: