LoginSignup
16
14

More than 3 years have passed since last update.

ディレクトリ内のファイル数を調べる

Last updated at Posted at 2020-10-10

DIR = './dataset'

print(sum(os.path.isfile(os.path.join(DIR, name)) for name in os.listdir(DIR)))

最初、下のように書いていたのですが、@shiracamusさんに

リストを作るとメモリを大量消費するので、sum関数でbool値を合計するといいですよ。

とコメントいただきました。ありがとうございます!

↓メモリ大量消費するやつ。

DIR = './dataset'
print(len([name for name in os.listdir(DIR) if os.path.isfile(os.path.join(DIR, name))]))

2933

🐣


お仕事のご相談こちらまで
rockyshikoku@gmail.com

Core MLを使ったアプリを作っています。
機械学習関連の情報を発信しています。

Twitter
Medium

16
14
2

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
16
14