1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Pythonでディレクトリの中の条件に合うファイルを取り出す

Last updated at Posted at 2019-08-03

リスト内包表記を使ってこんな風に書けば良い。

path = '任意のディレクトリ'
file = [i for i in os.listdir(path) if 条件]

例えば 'image' というディレクトリに入っている jpgファイルを取り出したかったら以下のようにすれば良い。
ただし jpgファイルでなくても '.jpg' という文字列が含まれていたらそのファイルも返してしまうので注意。

path = 'image/'
jpgfile = [i for i in os.listdir(path) if '.jpg' in i]
1
1
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
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?