5
4

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.

【Python】指定ディレクトリのファイル/フォルダサイズを一覧表示

Posted at

PCやネットワークドライブのストレージがいっぱいになって来た時、どこにどれくらい容量を使っているのか、パッと調べたいことがありました。その時作ったのがこちらです。

scan_dir_size.py
import os
import sys


def get_dir_size(path='.'):
    if not os.path.exists(path):
        print('存在しないPath: %s' % path)
        return 0

    total = 0
    try:
        with os.scandir(path) as it:
            for entry in it:
                if entry.is_file():
                    total += entry.stat().st_size
                elif entry.is_dir():
                    total += get_dir_size(entry.path)
    except FileNotFoundError as e:
        print(e)
    finally:
        return total


def scan_dir_size(dirPath):
    if not os.path.exists(dirPath):
        print('ディレクトリが存在しません')
        return

    filesAndDirs = os.listdir(dirPath)
    subDirs = [os.path.join(dirPath, f) for f in filesAndDirs
               if os.path.isdir(os.path.join(dirPath, f))]
    files = [os.path.join(dirPath, f) for f in filesAndDirs
             if os.path.isfile(os.path.join(dirPath, f))]

    sumSize = 0
    for f in files:
        sizeGb = os.path.getsize(f) / 1000 / 1000 / 1000
        print("filePath: %s, size: %.2f Gbyte" % (f, sizeGb))
        sumSize += sizeGb
    for folder in subDirs:
        sizeGb = get_dir_size(folder) / 1000 / 1000 / 1000
        print("dirPath: %s, size: %.2f Gbyte" % (folder, sizeGb))
        sumSize += sizeGb
    print("合計: %.2f Gbyte" % sumSize)


if __name__ == '__main__':
    argvs = sys.argv
    if len(argvs) == 2:
        scan_dir_size(argvs[1])
    else:
        print('引数にディレクトリのパスを設定してください。')

使い方は、

$ python scan_dir_size.py {サイズを調べたいディレクトリのパス}

引数にCドライブのパスを指定した場合、実行結果はこんな感じになります。

filePath: C:\hiberfil.sys, size: 3.37 Gbyte
filePath: C:\pagefile.sys, size: 9.13 Gbyte
filePath: C:\swapfile.sys, size: 0.02 Gbyte
dirPath: C:$RECYCLE.BIN, size: 9.49 Gbyte
dirPath: C:\Apache-Subversion-1.10.2, size: 0.01 Gbyte
dirPath: C:\Config.Msi, size: 0.00 Gbyte
dirPath: C:\cygwin64, size: 12.89 Gbyte
dirPath: C:\Documents and Settings, size: 0.00 Gbyte
dirPath: C:\LOG, size: 0.00 Gbyte
dirPath: C:\Fujitsu, size: 12.04 Gbyte
dirPath: C:\Intel, size: 0.00 Gbyte
dirPath: C:\MSOCache, size: 0.00 Gbyte
dirPath: C:\grin, size: 94.26 Gbyte
dirPath: C:\PerfLogs, size: 0.00 Gbyte
dirPath: C:\Program Files, size: 19.47 Gbyte
dirPath: C:\Program Files (x86), size: 8.84 Gbyte
dirPath: C:\ProgramData, size: 3.41 Gbyte
dirPath: C:\Qt, size: 49.30 Gbyte
dirPath: C:\Recovery, size: 0.00 Gbyte
dirPath: C:\Renesas, size: 0.00 Gbyte
dirPath: C:\System Volume Information, size: 0.00 Gbyte
dirPath: C:\Users, size: 62.22 Gbyte
dirPath: C:\Windows, size: 24.17 Gbyte
dirPath: C:\WorkSpace, size: 0.00 Gbyte
合計: 308.61 Gbyte

これで少なくとも一個一個プロパティを開く必要はなくなりましたな、、、

5
4
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
5
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?