Linux os.walk 没有隐藏文件夹

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13454164/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 17:49:40  来源:igfitidea点击:

os.walk without hidden folders

pythonlinuxos.walk

提问by lolopop

I need to list all files with the containing directory path inside a folder. I tried to use os.walk, which obviously would be the perfect solution.

我需要列出文件夹中包含目录路径的所有文件。我尝试使用os.walk,这显然是完美的解决方案。

However, it also lists hidden folders and files. I'd like my application not to list any hidden folders or files. Is there any flag you can use to make it not yield any hidden files?

但是,它还列出了隐藏的文件夹和文件。我希望我的应用程序不要列出任何隐藏的文件夹或文件。是否有任何标志可以用来使它不产生任何隐藏文件?

Cross-platform is not really important to me, it's ok if it only works for linux (.* pattern)

跨平台对我来说不是很重要,如果它只适用于 linux (.* 模式)

采纳答案by Martijn Pieters

No, there is no option to os.walk()that'll skip those. You'll need to do so yourself (which is easy enough):

不,没有选项可以os.walk()跳过这些。你需要自己这样做(这很容易):

for root, dirs, files in os.walk(path):
    files = [f for f in files if not f[0] == '.']
    dirs[:] = [d for d in dirs if not d[0] == '.']
    # use files and dirs

Note the dirs[:] =slice assignment; os.walkrecursively traverses the subdirectories listed in dirs. By replacing the elementsof dirswith those that satisfy a criteria (e.g., directories whose names don't begin with .), os.walk()will not visit directories that fail to meet the criteria.

注意dirs[:] =切片分配;os.walk递归遍历 中列出的子目录dirs。通过更换元件dirs与那些满足条件(例如,目录名称不开始.),os.walk()将不能访问不符合标准的目录。

This only works if you keep the topdownkeyword argument to True, from the documentation of os.walk():

这仅在您将topdown关键字参数保留为 时才有效True,来自 的文档os.walk()

When topdownis True, the caller can modify the dirnames list in-place (perhaps using delor slice assignment), and walk()will only recurse into the subdirectories whose names remain in dirnames; this can be used to prune the search, impose a specific order of visiting, or even to inform walk()about directories the caller creates or renames before it resumes walk()again.

topdownis 时True,调用者可以就地修改 dirnames 列表(可能使用del或切片赋值),并且walk()只会递归到名称保留在dirnames 中的子目录;这可用于修剪搜索,强加特定的访问顺序,甚至walk()walk()再次恢复之前通知调用者创建或重命名的目录。

回答by dmmfll

I realize it wasn't asked in the question, but I had a similar problem where I wanted to exclude both hidden files and files beginning with __, specifically __pycache__directories. I landed on this question because I was trying to figure out why my list comprehension was not doing what I expected. I was not modifying the list in place with dirnames[:].

我意识到问题中没有问到它,但我有一个类似的问题,我想排除隐藏文件和以 开头的文件__,特别是__pycache__目录。我提出这个问题是因为我试图弄清楚为什么我的列表理解没有达到我的预期。我没有用dirnames[:].

I created a list of prefixes I wanted to exclude and modified the dirnames in place like so:

我创建了一个我想排除的前缀列表,并像这样修改了目录名:

    exclude_prefixes = ('__', '.')  # exclusion prefixes
    for dirpath, dirnames, filenames in os.walk(node):
        # exclude all dirs starting with exclude_prefixes
        dirnames[:] = [dirname
                       for dirname in dirnames
                       if not dirname.startswith(exclude_prefixes)]

回答by James Dellinger

My use-case was similar to that of OP, except I wanted to return a count of the total number of sub-directories inside a certain folder. In my case I wanted to omit any sub-directories named .git(as well as any folders that may be nested inside these .gitfolders).

我的用例与 OP 的用例类似,只是我想返回某个文件夹内子目录总数的计数。就我而言,我想省略任何命名的子目录.git(以及可能嵌套在这些.git文件夹中的任何文件夹)。

In Python 3.6.7, I found that the accepted answer's approach didn't work -- it counted all .gitfolder and their sub-folders. Here's what did work for me:

在 Python 3.6.7 中,我发现接受的答案的方法不起作用——它计算了所有.git文件夹及其子文件夹。以下是对我有用的内容:

num_local_subdir = 0
for root, dirs, files in os.walk(local_folder_path):
    if '.git' in dirs:
        dirs.remove('.git')
    num_local_subdir += (len(dirs))