Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Directory Tree Generator

Here is an example of a directory tree generator you can use for inspiration:

Here are resources that you can use to build your directory tree generator:

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.

00:32 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.

00:57 This can be done with the ignore command,

01:01 and now you can see the relevant files and directories for this particular 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.

01:32 The main area of difference is the representation. You may want to use a library such as colorama or colored to color-code the output.

01:40 This would allow easy and quick visual differentiation between files and directories, and also, you may choose to use colors to represent file attributes, such as read-only.

01:53 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.

02:22 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.

02:39 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.

02:50 Many such parameters are implemented in tree, and it’s worth looking through the man page to see the kind of things that you could implement in your version.

Become a Member to join the conversation.