In Python, fastest way to build a list of files in a directory with a certain extension

后端 未结 6 1158
清酒与你
清酒与你 2021-01-12 08:34

In Python on a GNU/Linux system, what\'s the fastest way to recursively scan a directory for all .MOV or .AVI files, and to store them in a list? <

相关标签:
6条回答
  • 2021-01-12 08:50

    You can use os.walk() for recuresive walking and glob.glob() or fnmatch.filter() for file matching:

    Check this answer

    0 讨论(0)
  • 2021-01-12 08:58

    I'd use os.walk to scan the directory, os.path.splitext to grab the suffix and filter them myself.

    suffixes = set(['.AVI', '.MOV'])
    for dirpath, dirnames, filenames in os.walk('.'):
        for f in filenames:
            if os.path.splitext(f)[1] in suffixes:
                yield os.path.join(dirpath, f)
    
    0 讨论(0)
  • 2021-01-12 08:58
    pattern = re.compile('.*\.(mov|MOV|avi|mpg)$')
    
    def fileList(source):
       matches = []
       for root, dirnames, filenames in os.walk(source):
           for filename in filter(lambda name:pattern.match(name),filenames):
               matches.append(os.path.join(root, filename))
       return matches
    
    0 讨论(0)
  • 2021-01-12 08:58

    I suggest the use of os.walk and a carefully reading of its documentation.

    This may be a one liner approach:

    [f for root,dirs,files in os.walk('/your/path') for f in files if is_video(f)]
    

    Where in is_video you check your extensions.

    0 讨论(0)
  • Python 2.x:

    import os
    
    def generic_tree_matching(rootdirname, filterfun):
        return [
            os.path.join(dirname, filename)
            for dirname, dirnames, filenames in os.walk(rootdirname)
            for filename in filenames
            if filterfun(filename)]
    
    def matching_ext(rootdirname, extensions):
        "Case sensitive extension matching"
        return generic_tree_matching(
            rootdirname,
            lambda fn: fn.endswith(extensions))
    
    def matching_ext_ci(rootdirname, extensions):
        "Case insensitive extension matching"
        try:
            extensions= extensions.lower()
        except AttributeError: # assume it's a sequence of extensions
            extensions= tuple(
                extension.lower()
                for extension in extensions)
        return generic_tree_matching(
            rootdirname,
            lambda fn: fn.lower().endswith(extensions))
    

    Use either matching_ext or matching_ext_ci with arguments the root folder and an extension or a tuple of extensions:

    >>> matching_ext(".", (".mov", ".avi"))
    
    0 讨论(0)
  • 2021-01-12 09:02

    Example for a list of files in current directory. You can expand this for specific paths.

    import glob
    movlist = glob.glob('*.mov')
    
    0 讨论(0)
提交回复
热议问题