import os class OSdir: def __init__(self, path): self.path = path def esplora(self): self.files = [] self.dirs = [] if os.path.isdir(self.path): for filename in os.listdir(self.path): if filename[0] == '.': continue percorso_completo = os.path.join(self.path, filename) file = OSdir(percorso_completo) file.esplora() if os.path.isdir(percorso_completo): self.dirs.append(file) else: self.files.append(file) def __str__(self, livello=0): stringa = '| '*livello + "OSdir({})\n".format(self.path) for f in self.dirs: stringa += f.__str__(livello+1) for f in self.files: stringa += f.__str__(livello+1) return stringa def cerca(self, sottostringa, livello=0): print('| '*livello + "enter cerca({}, {})\n".format(self.path, sottostringa)) res = [] if sottostringa in self.path: res.append(self.path) for d in self.dirs: res += d.cerca(sottostringa, livello+1) for file in self.files: res += file.cerca(sottostringa, livello+1) print('| '*livello + "exit cerca({})\n".format(res)) return res if __name__ == '__main__': d = OSdir('.') d.esplora() print(d) x = d.cerca('png') print(x)