os.walk without digging into directories below

前端 未结 20 1202
庸人自扰
庸人自扰 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:03
    import os
    
    def listFiles(self, dir_name):
        names = []
        for root, directory, files in os.walk(dir_name):
            if root == dir_name:
                for name in files:
                    names.append(name)
        return names
    
    0 讨论(0)
  • 2020-12-04 07:05

    Use the walklevel function.

    import os
    
    def walklevel(some_dir, level=1):
        some_dir = some_dir.rstrip(os.path.sep)
        assert os.path.isdir(some_dir)
        num_sep = some_dir.count(os.path.sep)
        for root, dirs, files in os.walk(some_dir):
            yield root, dirs, files
            num_sep_this = root.count(os.path.sep)
            if num_sep + level <= num_sep_this:
                del dirs[:]
    

    It works just like os.walk, but you can pass it a level parameter that indicates how deep the recursion will go.

    0 讨论(0)
  • 2020-12-04 07:06

    root folder changes for every directory os.walk finds. I solver that checking if root == directory

    def _dir_list(self, dir_name, whitelist):
        outputList = []
        for root, dirs, files in os.walk(dir_name):
            if root == dir_name: #This only meet parent folder
                for f in files:
                    if os.path.splitext(f)[1] in whitelist:
                        outputList.append(os.path.join(root, f))
                    else:
                        self._email_to_("ignore")
        return outputList
    
    0 讨论(0)
  • 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().

    0 讨论(0)
  • 2020-12-04 07:10

    You could use os.listdir() which returns a list of names (for both files and directories) in a given directory. If you need to distinguish between files and directories, call os.stat() on each name.

    0 讨论(0)
  • 2020-12-04 07:10

    Since Python 3.5 you can use os.scandir instead of os.listdir. Instead of strings you get an iterator of DirEntry objects in return. From the docs:

    Using scandir() instead of listdir() can significantly increase the performance of code that also needs file type or file attribute information, because DirEntry objects expose this information if the operating system provides it when scanning a directory. All DirEntry methods may perform a system call, but is_dir() and is_file() usually only require a system call for symbolic links; DirEntry.stat() always requires a system call on Unix but only requires one for symbolic links on Windows.

    You can access the name of the object via DirEntry.name which is then equivalent to the output of os.listdir

    0 讨论(0)
提交回复
热议问题