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

Pyyhkäise näyttääksesi valikon

book
Challenge: Recursive File Search

Working with Nested Dictionaries

In Python, dictionaries can contain other dictionaries. This is useful for representing hierarchical structures like a file system.

To navigate a dictionary:

Use items() to iterate through all key-value pairs:

123456789101112
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 if a value is itself a dictionary (i.e., a folder, not a file):

123456789101112
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
Tehtävä

Swipe to start coding

Imagine needing to check whether a specific file exists within a nested dictionary structure representing a file system. Implement a function file_exists that recursively navigates through folders (dictionary objects) and searches for a file (represented by the string "file"). Return True if the file is found; otherwise, return False.

  1. Use a for loop to iterate through all elements of the file_system dictionary using the items() method. This retrieves the key (name) and the value (content).
  2. Check if content is a file (i.e., the string "file") and if name matches target (the name of the file being searched for).
  3. If both conditions are met, return True, indicating that the file has been found.
  4. If content is not a file, check whether it is a folder. Use the isinstance() function, passing content as the first argument and dict as the second (which checks if the element is a dictionary).
  5. If content is a folder, call file_exists recursively with the necessary parameters to continue searching inside it.
  6. If the recursive call returns True, the file has been found, so return True.
  7. If no matches are found after checking all folders and files, return False.

Ratkaisu

Switch to desktopVaihda työpöytään todellista harjoitusta vartenJatka siitä, missä olet käyttämällä jotakin alla olevista vaihtoehdoista
Oliko kaikki selvää?

Miten voimme parantaa sitä?

Kiitos palautteestasi!

Osio 5. Luku 2
single

single

Kysy tekoälyä

expand

Kysy tekoälyä

ChatGPT

Kysy mitä tahansa tai kokeile jotakin ehdotetuista kysymyksistä aloittaaksesi keskustelumme

close

Awesome!

Completion rate improved to 4.35

book
Challenge: Recursive File Search

Working with Nested Dictionaries

In Python, dictionaries can contain other dictionaries. This is useful for representing hierarchical structures like a file system.

To navigate a dictionary:

Use items() to iterate through all key-value pairs:

123456789101112
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 if a value is itself a dictionary (i.e., a folder, not a file):

123456789101112
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
Tehtävä

Swipe to start coding

Imagine needing to check whether a specific file exists within a nested dictionary structure representing a file system. Implement a function file_exists that recursively navigates through folders (dictionary objects) and searches for a file (represented by the string "file"). Return True if the file is found; otherwise, return False.

  1. Use a for loop to iterate through all elements of the file_system dictionary using the items() method. This retrieves the key (name) and the value (content).
  2. Check if content is a file (i.e., the string "file") and if name matches target (the name of the file being searched for).
  3. If both conditions are met, return True, indicating that the file has been found.
  4. If content is not a file, check whether it is a folder. Use the isinstance() function, passing content as the first argument and dict as the second (which checks if the element is a dictionary).
  5. If content is a folder, call file_exists recursively with the necessary parameters to continue searching inside it.
  6. If the recursive call returns True, the file has been found, so return True.
  7. If no matches are found after checking all folders and files, return False.

Ratkaisu

Switch to desktopVaihda työpöytään todellista harjoitusta vartenJatka siitä, missä olet käyttämällä jotakin alla olevista vaihtoehdoista
Oliko kaikki selvää?

Miten voimme parantaa sitä?

Kiitos palautteestasi!

close

Awesome!

Completion rate improved to 4.35

Pyyhkäise näyttääksesi valikon

some-alt