Super Simple Python: Python Directory Visualization

Python Directory Header

Do you want to be able to see all the directories, subdirectories, and files in your folder in a structured format? Let’s create a directory visualization tool. This post shows how to create a simple version of a directory visualization tool that is almost like the tree command from Unix. It’s a bit simpler, but can be extended to create a tree.

In this post we will cover:

  • Structuring Our Directory Information
  • Pretty Printing the Folders and Files in a Directory
  • Summary of a Python Directory Visualization

Structuring Our Directory Information

Our Python directory visualization tool consists of two steps. First, we structure our information, then we pretty print it. In this first step, we structure the directory information. We use the os module to walk our directory and get every file, folder, and subfolder in it. Next, we turn that data into a dictionary.

It’s important to know that os.walk returns a list of 3-tuples. Each entry in the list has three entries in it. First, we have the name of the top level folder, then the folders inside that directory, and thirdly, the files in there. (Note that folder and directory are synonymous)

We use this knowledge to create a logical dictionary representation. Each key entry in the dictionary is the name of a top level folder. The corresponding value is a dictionary that shows the folders in there and then the files in there.

# visualize a directory
import os
# produces a list of 3-tuples
dir = os.walk(".")
structure = {}
for p in dir:
   for entry in p:
       top_folder = p[0]
       folders = p[1]
       files = p[2]
       structure[top_folder] = {
           "Folders": folders,
           "Files": files

Pretty Printing the Folders and Files in a Directory

There are many ways we can opt to pretty print our info. In this post, we’re going to cover a super basic method. First, we loop through all the items in the dictionary representation of our directory. Next, we print out the top folder with no indents.

For each of these top folders, we also print out the folders and files inside it. We give the folders an indentation of one tab or four spaces. The files get an indentation of two tabs or eight spaces. It’s not as pretty as tree but gives you a basic idea of the directory. We may evolve this into a tree replacement command in a future post.

for top_folder, content in structure.items():
   print(f"Top Folder: {top_folder}")
   for folder, files in content.items():
       print(f"    Folders: {folder}")
       print(f"        Files: {files}")

The print out will look like this:

Python Directory Visualization Example
Python Directory Visualization Example

Summary of Python Directory Visualization

Directory visualization isn’t as hard as it seems. Although neither Windows computers nor Macs come with tree, we can create a rough emulator. The directory visualization program we created today relies on os.walk to get all the information of a directory. Then we put that information into a dictionary, and print out the logical structure of that.

I run this site to help you and others like you find cool projects and practice software skills. If this is helpful for you and you enjoy your ad free site, please help fund this site by donating below! If you can’t donate right now, please think of us next time.

Yujian Tang

Leave a Reply

%d bloggers like this: