Iterate list contents and find json files

Kind of breaking my head to find an elegant solution for the below scenario.

A list has directory structure, as its contents & need to find whether request.json or response.json is available in the result folder of the directory structure or one level below the result folder, called – scheduler.

Let’s say:

input_paths = ['/root/services/builds/tesla', '/root/services/builds/google/apis', '/root/services/builds/qa/tests', '/root/services/builds/airlines_Solutions', '/root/services/builds/traffic_patterns/api']

Output should be:




My code has multiple for loops that has os.walk and globs and looks pathetic. Looking forward to learn and understand a simple solution. Thanks.


You could simply use os.exists

import os

for i in input_paths:
    if os.path.exists(os.path.join(i, "result/request.json")):
        print(os.path.join(i, "result/request.json"))

    elif os.path.exists(os.path.join(i, "result/scheduler/request.json")):
        print(os.path.join(i, "result/scheduler/request.json"))