美文网首页程序员
使用Generator和Recursion遍历目录

使用Generator和Recursion遍历目录

作者: PythonDeveloper | 来源:发表于2016-08-09 13:02 被阅读27次

    在遍历或搜索文件系统时,如果目录的文件很多,目录结果复杂,可能会消耗较多的内存来保存已遍历的信息,所以使用Generator就是一个比较好的选择。另外,使用Recursion会降低代码量,使代码可读性更强。

    遍历目录,返回所有的文件和目录

    def scan_dir_gen(dir_path):
        """Scan directory recursively and yield all files and directories.
    
        Args:
            dir_path: Path object to be scanned.
        """
        if dir_path is None or not isinstance(dir_path, Path):
            raise TypeError('dir_path must be a Path object')
    
        # The current directory.
        yield dir_path
        for path in dir_path.iterdir():
            if path.is_dir():
                # Recursively tranverse the directory.
                yield from scan_dir_gen(path)
            else:
                yield path
    

    遍历目录,只返回只有文件的目录

    def dir_only_has_files_gen(dir_path):
        """Scan the directory recursively and returns directories which contain only files.
    
        Args:
            dir_path: Path object to be scanned.
        """
        if dir_path is None or not isinstance(dir_path, Path):
            raise TypeError('dir_path must be a Path object')
    
        has_dir = False
        for path in dir_path.iterdir():
            if path.is_dir():
                has_dir = True
                yield from dir_only_has_files_gen(path)
        if not has_dir:
            yield dir_path
    

    相关文章

      网友评论

        本文标题:使用Generator和Recursion遍历目录

        本文链接:https://www.haomeiwen.com/subject/ahcdsttx.html