path = '/home/junha/Projects/foundry/' import os def FindFile(path, format): result = [] for f in os.listdir(path): x = path + f if os.path.isdir(x): result += FindFile(x + '/', format) elif(f.split('.')[-1] == format): result.append(x) return result def FindPos(file: str): x = file.split('\n') n = len(x) result = [] for i in range(n): if i >= n - 2: break if x[i][0:3] == 'use' and x[i+2][0:3] == 'use' and x[i+1] == '': result.append(i+1) return result import typing def ApplyWork(file: str, pos: typing.List[int]): x = file.split('\n') for i in sorted(pos, reverse = True): del x[i] result = "" for l in x: result += l + '\n' return result[:-1] for x in FindFile(path, 'rs'): res = None with open(x, 'r') as f: s = f.read() res = ApplyWork(s, FindPos(s)) with open(x, 'w') as f: f.write(res)