简体   繁体   English

如何阻止 os.walk 向下遍历所有子目录?

[英]How to stop os.walk from walking all sub-directories down?

Using:使用:

def simpleFunc(dirName):
    import os
    fileList=[]
    for dir, dirs, filenames in os.walk(dirName):
        for filename in filenames:
            filepath=dir+'/'+filename
            fileList.append(filepath)
    print fileList


simpleFunc(os.path.dirname('/entire/path/to/file.ext'))

The problem is that os.walk just doesn't stop at /entire/path/to/ directory level but goes all the way down to the lowest sub-directory it can find.问题是os.walk并没有停止在/entire/path/to/目录级别,而是一直到它可以找到的最低子目录。 So if there is /entire/path/to/subdir1/subdir2/subdir3 then in additional to /entire/path/to/ all three sub-directories will be searched: ./subdir1/ , ././subdir2/ , ./././subdir3/ .因此,如果有/entire/path/to/subdir1/subdir2/subdir3那么除了/entire/path/to/所有三个子目录都将被搜索: ./subdir1/ , ././subdir2/ , ./././subdir3/ . Question: how to make sure the function stops at the directory level specified: /entire/path/to/ and doesn't go all the way down?问题:如何确保函数在指定的目录级别停止: /entire/path/to/并且不会一直向下?

from os import listdir
from os.path import isfile, join
path='/entire/path/to/'
files = [ join(path,f) for f in listdir(path) if isfile(join(path,f)) ]

files return ur files.文件返回你的文件。 no need to declare new filelist无需声明新的文件列表

Based on what you've written, if you just want to search the specified directory.根据您编写的内容,如果您只想搜索指定的目录。 It might be better to use os.listdir and then just filter on os.path.isfile , eg, like this:使用os.listdir然后只过滤os.path.isfile可能会更好,例如,像这样:

def simple_func(dirpath):
    paths = os.listdir(dirpath)
    # create absolute paths
    abs_paths = [os.path.join(dirpath, p) for p in paths]
    # filter for paths that are files
    file_list = [p for p in paths if os.path.isfile(p)]
    return file_list

That way you don't have to deal with stopping the recursion and it's pretty clear which files you want.这样您就不必处理停止递归的问题,并且很清楚您想要哪些文件。 You might want to profile to see whether the multiple isfile calls hurt anything.您可能想要分析以查看多个isfile调用是否会造成任何伤害。

Sounds like you should use os.listdir instead.听起来您应该改用os.listdir

import os

my_path = '/entire/path/to/files/'
file_list = []
for filename in os.listdir(my_path):
  filepath = os.path.join(my_path, filename)
  if os.path.isfile(filepath):
    fileList.append(filepath)

You need to take the only first item from the os.walk generator:您需要从os.walk生成器中获取唯一的第一项:

import os

root, _, files = next(os.walk('/entire/path/to'))

and then append root dir to each filename:然后将根目录附加到每个文件名:

files = map(lambda f: os.path.join(root, f), files)

or just:要不就:

files = [os.path.join(root, f) for f in files]

You can tell os.walk which directory to use explicitly您可以告诉 os.walk 明确使用哪个目录

def simpleFunc(dirName):
    import os

    # set the dir name in a var
    dirName = '/higher_lever_dir/'

    for dir, dirs, filenames in os.walk(dirName):

        # this line forces loop to look ONLY in the specified dir
        dirs[:] = [d for d in dirs if d in dirName]

        for filename in filenames:
            filepath=dir+'/'+filename
            fileList.append(filepath)
    print fileList


simpleFunc(os.path.dirname('/entire/path/to/file.ext'))

Setting dirs[:] = [d for d in dirs if d in dirName] will exclude files in subdirectories that may exist in /higher_level_dir/设置 dirs[:] = [d for d in dirs if d in dirName] 将排除 /higher_level_dir/ 中可能存在的子目录中的文件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM