Message 363722 - Python tracker (original) (raw)

It is not so easy. There was reason why it was not done earlier. scandir() wastes more limited resource than memory -- file descriptors. It should also be properly closed and do not depend on the garbage collector.

Consider the example:

def traverse(path, visit): for child in path.iterdir(): if child.is_dir(): traverse(path, visit) else: visit(child)

With your optimization it may fail with OSError: Too many open files.