0

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:

/root/services/builds/tesla/result/request.json

/root/services/builds/google/apis/result/scheduler/request.json

/root/services/builds/qa/tests/result/scheduler/response.json

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.

Chel MS
  • 386
  • 1
  • 10

2 Answers2

1

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"))
sushanth
  • 8,275
  • 3
  • 17
  • 28
1

To recursively find all request.json or response.json in all you input_paths you could do:

import os

file_name_pattern = ['request.json', 'response.json', ]
for path in input_paths:
    for root, dirs, files in os.walk(path):
        for name in files:
            if name in file_name_pattern:
                print(os.path.join(root, name))

If the directory structure is mandatory to your findings you should add another filter for dirs.

The answer of sushanth might be more efficient due to prevent the algorithm to search recursively but it might be a hassle to configure the code if more search locations become relevant.

tharndt
  • 127
  • 3
  • 9