import os #rtdir = '/srv/ftp/hummingbird/2021/07/21' rtdir = os.getcwd() files_read = list() for cdir, _, files in os.walk(rtdir): for f in files: if f.endswith('.json'):# and 'trimmed' not in f: files_read.append(os.path.join(cdir, f)) import json fcontents = dict() for f in files_read: with open(f) as ff: try: fcontents[f] = json.load(ff) except: print(f, ' Failed') to_purge = list() for fname, contents in fcontents.items(): if len(contents) > 0: nscores = list() for x in contents: if len(x['scores']) > 0: nscores.append(max(x['scores'])) if len(nscores) == 0 or max(nscores) < 0.60: to_purge.append(fname) delete_list = set() for f_json in to_purge: vid_path = f_json.rstrip('.json') + '.mp4' img_path = vid_path.rstrip('.mp4')+'.jpg' if not os.path.exists(img_path) and 'trimmed' not in img_path: img_path_sp = img_path.split('_') img_path_sp[-1] = str(int(img_path_sp[-1].rstrip('.jpg'))+1) + '.jpg' img_path = '_'.join(img_path_sp) delete_list.update({vid_path,img_path, f_json}) print(delete_list) for f in delete_list: if os.path.exists(f): os.remove(f)