更实用的os.walk

发布于 2024-12-19 01:30:50 字数 1459 浏览 0 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

狠疯拽 2024-12-26 01:30:50

您需要就地修改目录以避免递归遍历已删除的目录。使用:

dirs[:] = [d for d in dirs if pred_dir(path.join(base, d))]

这将消除检查 _ignore_dirs_predicate(base) 的需要(并消除由于使用 _ignore_dirs_predicate 而不是 导致的 NameError pred_dir

您还应该重写 ISA_PY 以使用 str.endswith()

you need to modify dirs in place in order to avoid recursive traversal of the removed directories. Use:

dirs[:] = [d for d in dirs if pred_dir(path.join(base, d))]

this will remove the need to check _ignore_dirs_predicate(base) (and remove the NameError caused by the use of _ignore_dirs_predicate instead of pred_dir)

You should also rewrite ISA_PY to use str.endswith()

江城子 2024-12-26 01:30:50

是的。原来的界面有什么问题吗?将谓词应用于原始输出以获得最佳结果。除了 os.path.join(base, name) 之外,没有太多改进的空间。

Yeah. What's wrong with the original interface? Apply your predicates to the raw output to get the best results. Beyond an os.path.join(base, name), there's not much room to improve.

陌路终见情 2024-12-26 01:30:50

好吧,实际上我意识到我做错了(正如我怀疑的那样)。
问题是,大多数时候我不知道是否要删除路径,直到获得完整路径。

例如,给定:

r1:
    -d1
        + test: file1, file2
r2:
    - d2

我只能知道当我拥有完整路径“r1/d1/test/file1”时我想对该文件做什么,因此尝试在它使生活变得更加复杂之前进行过滤。

这就是现在的样子,

def walk_setup_py(src):
    """Iterate through the setup.py files
    """
    return walk_full_path_files(src, pred_file=ISA_SETUP_PY)


# TODO: this should be the default behaviour
def walk_full_path_files(src, pred_dir=_default_dir_predicate,
                         pred_file=_default_file_predicate):
    """Iterates on the full path given a walker function
    """
    for root, _, files in walk(src):
        for f in files:
            fpath = path.join(root, f)
            if pred_dir(root) and pred_file(f):
                yield fpath

比我以前做的要好得多..

Ok actually I realized that I was doing it all wrong (as I suspected).
The problem is that most of the time I don't know if I want to drop a path or not until I have the complete path.

So for example, given:

r1:
    -d1
        + test: file1, file2
r2:
    - d2

I can only know when I have the full path "r1/d1/test/file1" what I want to do with that file, so trying to filter before it makes life a lot more complicated.

This is how it becomes now

def walk_setup_py(src):
    """Iterate through the setup.py files
    """
    return walk_full_path_files(src, pred_file=ISA_SETUP_PY)


# TODO: this should be the default behaviour
def walk_full_path_files(src, pred_dir=_default_dir_predicate,
                         pred_file=_default_file_predicate):
    """Iterates on the full path given a walker function
    """
    for root, _, files in walk(src):
        for f in files:
            fpath = path.join(root, f)
            if pred_dir(root) and pred_file(f):
                yield fpath

Which is way nicer than what I was doing before..

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文