Advertisement
Guest User

Untitled

a guest
Feb 18th, 2020
256
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.68 KB | None | 0 0
  1. import json
  2.  
  3. with open('data.json', 'r') as f:
  4.     data = json.load(f)
  5.  
  6. target = 'files/pasta-1/testes.html'
  7.  
  8.  
  9. def find_target(target, data, current_path=''):
  10.     for item in data:
  11.         if item['type'] == 'file' and f'{current_path}/{item["name"]}' == target:
  12.             return item
  13.         else:
  14.             if item.get('items', []):
  15.                 if current_path:
  16.                     current_path = f'{current_path}/{item["name"]}'
  17.                 else:
  18.                     current_path = f'{item["name"]}'
  19.                 return find_target(
  20.                     target, item['items'], current_path=current_path)
  21.     return None
  22.  
  23.  
  24. print(find_target(target, [data]))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement