os.walk without digging into directories below

前端 未结 20 1310
庸人自扰
庸人自扰 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:07

    There is a catch when using listdir. The os.path.isdir(identifier) must be an absolute path. To pick subdirectories you do:

    for dirname in os.listdir(rootdir):
      if os.path.isdir(os.path.join(rootdir, dirname)):
         print("I got a subdirectory: %s" % dirname)
    

    The alternative is to change to the directory to do the testing without the os.path.join().

提交回复
热议问题