Directory Tree Generator
Here is an example of a directory tree generator you can use for inspiration:
- tree command: Wikipedia article about the recursive directory listing command
- tree: Homebrew install for macOS Display directories as trees (with optional color/HTML output)
Here are resources that you can use to build your directory tree generator:
- os Module: Miscellaneous operating system interfaces
- sys Module: System-specific parameters and functions
- shutil Module: High-level file operations
- Working With Files in Python: Real Python article
- argparse Module: Parser for command-line options, arguments and sub-commands
- How to Build Command Line Interfaces in Python With argparse: Real Python article
- click: A Python package for creating beautiful command line interfaces
- docopt: Command-line interface description language
- colorama: Cross-platform colored terminal text
- pillow: The friendly PIL (Python Imaging Library) fork
00:00 Now, let’s look at the final project in the command-line section of the course: a directory tree generator. You may be aware that most directories are arranged in a tree structure and seeing the relationship between them can be important, particularly when working on programming projects.
00:16 A directory tree generator will allow you to see the relationship between all of the files and directories in that tree, making it easy to understand the positioning of files and directories and see what’s going on, either in your project or in a larger part of your computer’s hard drive.
Let’s take a look at an implementation of a directory tree generator using the
tree command, which is available in Windows, DOS, and is installed on macOS and Linux using
homebrew. Simply typing the command
tree will generate a tree for the current directory. However, in this case, I have a Python virtual environment, which I want to ignore to show the most important parts of this current tree.
01:07 Let’s take a look at some of the technical details that you’ll need to implement your directory tree generator. A number of these will be common to the previous bulk file rename tool project, so you may find your experience there makes this easier and simpler to implement. Firstly, you’ll need to access files using the same libraries as seen previously, and you’re also going to need to accept arguments in much the same way.
Now, let’s take a look at some of the extra challenges you could take on when you’ve implemented your basic directory tree generator, the first of which is graphical output using a library such as
pillow. While text-based output is easy and quick to implement—and for small structures, can make sense—when trying to map an entire drive, the output may become unmanageably large and difficult to interpret. Producing a large graphic of this kind of output may be much more appropriate.
Secondly, file representations. As you’ve seen,
tree merely reports the name of the file or directory and color-codes them. An extension of this is to add icons or other methods to represent different files to the user. The third is user-controlled parameters.
As you saw in the demonstration of
tree, being able to ignore a particular pattern of directories meant I could see only what was relevant to me without seeing the contents of my Python virtual environment.
Become a Member to join the conversation.