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 5 years have passed since last update.

フォルダのリストが書かれたテキストファイルからフォルダのファイル数を取得する。

Posted at
fileList.txt
C:\exist1
C:\noexist
C:\exist2
C:\exist3

上記のようなフォルダパスが書かれたテキストファイルを読み込み、そのフォルダにあるファイル数をそれぞれ取得する。

fileCounter.py
import os, os.path
folders = open("fileList.txt", "r")

for folder in folders:
    if os.path.exists(str(folder).rstrip()):#読み込めるフォルダ内のファイル数を表示
       filenum=len(os.listdir(str(folder).rstrip()))
       print (folder)
       print (filenum)
    else: #読み込めないフォルダを表示
        print("can not read path")
        print(folder)
folders.close()
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?