find.py 文件源码

python
阅读 25 收藏 0 点赞 0 评论 0

项目:unitils 作者: iLoveTux 项目源码 文件源码
def find(path=".", name=None, iname=None, ftype="*"):
    """Search for files in a directory heirarchy.

    This is dramatically different from the GNU version of
    find. There is no Domain Specific language.

    :param path: The directory to start in
    :param name: The name spec (glob pattern) to search for
    :param iname: The case-insensitive name spec (glob pattern) to search for
    :param ftype: The type of file to search for must be one of b, c, d, p, f, k, s or *
    :type ftype: str
    :type iname: str
    :type name: str
    :type path: str
    """
    if ftype not in "bcdpfls*" or len(ftype) != 1:
        raise NotImplementedError(
            "Introspection for {} not implemented".format(ftype)
        )
    ftype_mapping = {
        "b": stat.S_ISBLK, "c": stat.S_ISCHR,
        "d": stat.S_ISDIR, "p": stat.S_ISFIFO,
        "f": stat.S_ISREG, "l": stat.S_ISLNK,
        "s": stat.S_ISSOCK,
        "*": lambda *args, **kwargs: True,
    }
    type_test = ftype_mapping[ftype]

    if name is not None:
        regex = re.compile(fnmatch.translate(name))
    elif iname is not None:
        regex = re.compile(fnmatch.translate(iname), flags=re.IGNORECASE)
    else:
        regex = re.compile(fnmatch.translate("*"))

    if regex.match(path) and type_test(os.stat(path).st_mode):
        yield os.path.relpath(path)

    for root, dirs, files in os.walk(path):
        for n in files + dirs:
            filename = os.path.join(root, n)
            _stat = os.stat(filename)
            if regex.match(n) and type_test(_stat.st_mode):
                yield filename
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号