-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathWinDirSize.py
More file actions
50 lines (40 loc) · 1.26 KB
/
WinDirSize.py
File metadata and controls
50 lines (40 loc) · 1.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# -*- coding: utf-8 -*-
"""
Created on Thu Jul 30 14:44:02 2015
@author: admingh
"""
import locale
import os
locale.setlocale(locale.LC_ALL, "")
def get_size(state, root, names):
paths = [os.path.realpath(os.path.join(root, n)) for n in names]
# handles dangling symlinks
state[0] += sum(os.stat(p).st_size for p in paths if os.path.exists(p))
def print_sizes(root):
total = 0
paths = []
state = [0]
n_ind = s_ind = 0
for name in sorted(os.listdir(root)):
path = os.path.join(root, name)
if not os.path.isdir(path):
continue
state[0] = 0
os.path.walk(path, get_size, state)
total += state[0]
s_size = locale.format('%8.0f', state[0], 3)
n_ind = max(n_ind, len(name), 5)
s_ind = max(s_ind, len(s_size))
paths.append((name, s_size))
for name, size in paths:
print name.ljust(n_ind), size.rjust(s_ind), 'bytes'
s_total = locale.format('%8.0f', total, 3)
print '\ntotal'.ljust(n_ind), s_total.rjust(s_ind), 'bytes'
print_sizes('.')
#import win32com.client as com
#
#folderPath = r"E:\WORK\Python\PythonScripts"
#fso = com.Dispatch("Scripting.FileSystemObject")
#folder = fso.GetFolder(folderPath)
#MB=1024*1024.0
#print "%.2f MB"%(folder.Size/MB)