Daily Python Projects

Daily Python Projects

Share this post

Daily Python Projects
Daily Python Projects
Build a Nested Dictionary from Folders and Files in Python

Build a Nested Dictionary from Folders and Files in Python

Level: Intermediate

Ardit Sulce's avatar
Ardit Sulce
Jul 30, 2025
∙ Paid
4

Share this post

Daily Python Projects
Daily Python Projects
Build a Nested Dictionary from Folders and Files in Python
Share

Your task for today is to write a Python program that scans folders representing classes (like "math", "science", etc.), reads all the .txt files in each folder, and builds a nested dictionary containing the file contents. This project will help you practice working with folders, files, and nested data structures — all very common in real-world automation and data collection.

📝 Project Task

The program should:

  • Start with a main folder (e.g. students/), where each subfolder represents a subject (math/, science/, etc.).

  • Inside each subject folder, there are text files for each student (alice.txt, bob.txt, etc.).

  • Read the content of each file.

  • Build a dictionary that looks like this:

{
  "math": {
    "alice.txt": "Alice's math notes here",
    "bob.txt": "Bob's math notes here"
  },
  "science": {
    "alice.txt": "Alice's science notes here"
  }
}

This is a very useful pattern for gathering, organizing, and transforming text data from folders into structured Python dictionaries.

📌 Expected Output

The output should be a dictionary where each key is a folder name (like math, science) and each value is another dictionary mapping filenames to their content.

For example, given this structure:

students/
├── math/
│   ├── alice.txt
│   └── bob.txt
├── science/
│   └── alice.txt

Your program should output something like:

{
  "math": {
    "alice.txt": "I love calculus.",
    "bob.txt": "Geometry is fun."
  },
  "science": {
    "alice.txt": "Physics is fascinating."
  }
}

💬 Join the Weekly Python Chat

Need help or want to share what you built? Join this week’s Python Chat Thread to:

  • Ask questions

  • Share your solution

  • Get feedback

  • Find study partners

Join Weekly Chat


💻 Launch This Project in Colab

Open the interactive Google Colab notebook for today’s project — with full instructions, hints, and solutions.

🔒 Click the button below to start coding — no setup needed:

This post is for paid subscribers

Already a paid subscriber? Sign in
© 2025 Ardit Sulce
Privacy ∙ Terms ∙ Collection notice
Start writingGet the app
Substack is the home for great culture

Share