forked from ustcwpz/USTC-CS-Courses-Resource
-
Notifications
You must be signed in to change notification settings - Fork 0
/
getSize.py
30 lines (27 loc) · 796 Bytes
/
getSize.py
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
# coding: utf-8
import os
import sys
def formatSize(size):
s = 'BKMGTP'
ct = 0
while size>=(1<<ct):
ct+=10
if ct>=10: ct-=10
return '{sz:.2f}{a}'.format(sz=size/(1<<ct),a=s[ct//10])
def getSize(path='.'):
if os.path.isdir(path):
gen = os.walk(path)
li = []
for root, dirs, files in gen:
for f in files:
sz = os.path.getsize(os.path.join(root ,f))
li.append(sz)
#li.insert(('.',sum(i[1] for i in li)),0)
#size = [f'{i[0]}: {formatSize(i[1])}' for i in li]
return formatSize(sum(li))
else:
return formatSize(os.path.getsize(path))
if __name__ == "__main__":
items = sys.argv[1:]
for i in items:
print('{i}: {sz}'.format(i=i,sz =getSize(i)))