Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Challenge: Recursive File Search | Recursion and Lambda Functions
Python Functions Tutorial

bookChallenge: Recursive File Search

Working with Nested Dictionaries

Dictionaries can store other dictionaries, which is useful for representing hierarchical data like a file system.

  • Keys are file or folder names;
  • "file" means the item is a file;
  • A nested dictionary means the item is a folder.

Use items() to iterate through key–value pairs.

1234567
file_system = { "home": {"resume.pdf": "file", "notes.txt": "file"}, "etc": {"config.yaml": "file"} } for name, content in file_system.items(): print(name, "->", content)
copy

Use isinstance(content, dict) to check whether a value represents a folder:

12345678910
file_system = { "home": {"resume.pdf": "file"}, "config.yaml": "file" } for name, content in file_system.items(): if isinstance(content, dict): print(name, "is a folder") else: print(name, "is a file")
copy
Task

Swipe to start coding

To check whether a specific file exists in a nested dictionary, implement a recursive file_exists function:

  1. Loop through the dictionary with items().
  2. If content is "file" and name matches target, return True.
  3. If content is a folder (isinstance(content, dict)), call file_exists recursively.
  4. If the recursive call returns True, propagate True.
  5. If nothing matches after checking all levels, return False.

Solution

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 5. ChapterΒ 2
single

single

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

Suggested prompts:

Can you explain how to add a new file or folder to this nested dictionary?

How can I recursively print all files and folders in this structure?

What does the output of these code samples look like?

close

Awesome!

Completion rate improved to 4.17

bookChallenge: Recursive File Search

Swipe to show menu

Working with Nested Dictionaries

Dictionaries can store other dictionaries, which is useful for representing hierarchical data like a file system.

  • Keys are file or folder names;
  • "file" means the item is a file;
  • A nested dictionary means the item is a folder.

Use items() to iterate through key–value pairs.

1234567
file_system = { "home": {"resume.pdf": "file", "notes.txt": "file"}, "etc": {"config.yaml": "file"} } for name, content in file_system.items(): print(name, "->", content)
copy

Use isinstance(content, dict) to check whether a value represents a folder:

12345678910
file_system = { "home": {"resume.pdf": "file"}, "config.yaml": "file" } for name, content in file_system.items(): if isinstance(content, dict): print(name, "is a folder") else: print(name, "is a file")
copy
Task

Swipe to start coding

To check whether a specific file exists in a nested dictionary, implement a recursive file_exists function:

  1. Loop through the dictionary with items().
  2. If content is "file" and name matches target, return True.
  3. If content is a folder (isinstance(content, dict)), call file_exists recursively.
  4. If the recursive call returns True, propagate True.
  5. If nothing matches after checking all levels, return False.

Solution

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 5. ChapterΒ 2
single

single

some-alt