Read a Whole Folder

Python for AIMedium+10 XP+10

Read a Whole Folder

A folder holds a few small text files. You want all of them in one dictionary.

Write folder_contents(folder). Return a dictionary that maps each file name in the folder to that file's text, with .strip() applied.

  • Use os.listdir to find the file names.
  • An empty folder gives {}.

The starter has a helper make_folder(folder, files) that builds a test folder from a dictionary and returns the folder name. Leave it as it is.

Examples

folder = make_folder('pets', {'cat.txt': 'tabby\n', 'dog.txt': 'collie\n'})
returns {'dog.txt': 'collie', 'cat.txt': 'tabby'}
folder = make_folder("fc_a", {"x.txt": "first\n", "y.txt": "second\n"})
returns {'y.txt': 'second', 'x.txt': 'first'}
folder = make_folder("fc_b", {"note.txt": " hello \n"})
returns {'note.txt': 'hello'}

Hints

Solve it to unlock the solution

Once your code passes every test, a reference solution shows up here so you can compare it with yours.

solution.py
CtrlEnterto run