os.walk without digging into directories below

前端 未结 20 1203
庸人自扰
庸人自扰 2020-12-04 06:21

How do I limit os.walk to only return files in the directory I provide it?

def _dir_list(self, dir_name, whitelist):
    outputList = []
    for         


        
相关标签:
20条回答
  • 2020-12-04 07:20

    You could also do the following:

    for path, subdirs, files in os.walk(dir_name):
        for name in files:
            if path == ".": #this will filter the files in the current directory
                 #code here
    
    0 讨论(0)
  • 2020-12-04 07:21

    The same idea with listdir, but shorter:

    [f for f in os.listdir(root_dir) if os.path.isfile(os.path.join(root_dir, f))]
    
    0 讨论(0)
提交回复
热议问题