LoginSignup
0
0

More than 1 year has passed since last update.

【Python】複数拡張子を指定して、ファイル一覧を取得する

Posted at

概要

特定のディレクトリから、複数の拡張子を対象としてファイル一覧を取得したい場合のメモ。

特定のディレクトリに格納されているファイル一覧を取得したい際には、globモジュールを使用する。

ソースコード

import glob

file_list = [] # 対象ファイル一覧
TARGET_EXTENSIONS = ("mp4", "mov", "avi") # 対象拡張子

for ext in TARGET_EXTENSIONS:
  # 取得対象のファイルパス(今回は./hogeを対象とする)
  target_filepath = f"./hoge/*.{ext}"
  # ファイル一覧取得
  file_list.extend(glob.glob(target_filepath))

おわり

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0