10.11.20 11:07, Manfred Lotz пише: > Perhaps better. I like to use os.scandir this way > > def scantree(path: str) -> Iterator[os.DirEntry[str]]: > """Recursively yield DirEntry objects (no directories) > for a given directory. > """ > for entry in os.scandir(path): > if entry.is_dir(follow_symlinks=False): > yield from scantree(entry.path) > > yield entry > > Worked fine so far. I think I coded it this way because I wanted the > full path of the file the easy way.
If this simple code works to you, that's fine, use it. But there are some pitfalls: 1. If you add or remove entries while iterate the directory, the behavior is not specified. It depends on the OS and file system. If you do not modify directories -- all okay. 2. As Cameron said, this method holds open file descriptors for all parent directories. It is usually not problem, because the maximal depth of directories is usually less than the limit of open files. But it can be problem if you process several trees in parallel. The common mistake also to not handle symlinks, but your code does it correctly. -- https://mail.python.org/mailman/listinfo/python-list