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.

Learn More

To learn more, feel free to reach out to me @yujian_tang on Twitter, connect with me on LinkedIn, and join our Discord. Remember to follow the blog to stay updated with cool Python projects and ways to level up your Software and Python skills! If you liked this article, please Tweet it, share it on LinkedIn, or tell your friends!

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
Yujian Tang

I started my professional software career interning for IBM in high school after winning ACSL two years in a row. I got into AI/ML in college where I published a first author paper to IEEE Big Data. After college I worked on the AutoML infrastructure at Amazon before leaving to work in startups. I believe I create the highest quality software content so that’s what I’m doing now. Drop a comment to let me know!

One-Time
Monthly
Yearly

Make a one-time donation

Make a monthly donation

Make a yearly donation

Choose an amount

$5.00
$15.00
$100.00
$5.00
$15.00
$100.00
$5.00
$15.00
$100.00

Or enter a custom amount

$

Your contribution is appreciated.

Your contribution is appreciated.

Your contribution is appreciated.

DonateDonate monthlyDonate yearly

Leave a Reply

%d bloggers like this: