Renaming a File in PyTown

In the virtual world of PyTown, every digital document or file is stored in directories and sub-directories that are represented as nested dictionaries. The town mayor, Mr. CeePy, has a major responsibility to maintain all these documents organized, named properly, and easily accessible. One day, Mr. CeePy realized a very important file was misnamed, and he needs to correct it urgently.
Your task is to help Mr. CeePy by writing a recursive Python program to search for this specific file in the nested dictionary directory structure and rename it if found. Here, keys represent folder names or file names, and the values are either dictionaries representing sub-folders or strings representing file extensions.
The first line of the input is the nested dictionary structure. This dictionary represents directories as keys and either a dictionary or a string as values. The strings are file extensions that, when combined with their keys (filenames), form the complete file name.
The second line of the input is the existing filename (with its extension) that needs to be found and renamed.
The third line of the input is the new filename that the existing file should be renamed to.
The program should return the modified nested dictionary structure with the renamed file. If the file is not found in the directory structure, the program should print File not found.
{"home": {"documents": {"report": ".docx", "data": ".csv", "analysis": ".txt"}, "pictures": {"beach": ".jpg", "portrait": ".png"}}, "desktop": {"important": ".pdf", "todo": ".txt"}} important.pdf urgent.pdf
{"home": {"documents": {"report": ".docx", "data": ".csv", "analysis": ".txt"}, "pictures": {"beach": ".jpg", "portrait": ".png"}}, "desktop": {"urgent": ".pdf", "todo": ".txt"}}


Time limit: 2 seconds

Memory limit: 512 MB

Output limit: 1 MB

To check your solution you need to sign in
Sign in to continue