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

后端 未结 6 1157
清酒与你
清酒与你 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: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
    

提交回复
热议问题