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

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

    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)
    

提交回复
热议问题