-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
48 lines (41 loc) · 1.23 KB
/
main.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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import os
import logging
from os.path import join, getsize
import re
def bytes2human(size):
gibi = 1024*1024*1024
mibi = 1024*1024
kibi = 1024
if size >= gibi:
return str(round(size/gibi, 2))+" GiB"
elif size >= mibi:
return str(round(size/mibi, 2))+" MiB"
elif size >= kibi:
return str(round(size/kibi, 2))+" KiB"
else:
return str(size)+" Bytes"
def clearsize(foldername):
match = re.search("\(\d+(\.\d+)?.(Bytes|GiB|MiB|KiB)\)",
foldername)
if match:
stripped = \
foldername[:match.start()] + foldername[match.end():]
# print("Stripped: "+stripped)
return stripped
else:
return foldername
for root, dirs, files in os.walk(os.curdir, topdown=True):
rootname = os.getcwd()
size = 0
for name in files:
filepath = join(root, name)
size += getsize(filepath)
try:
desc = '('+bytes2human(size)+')'
cleared = clearsize(rootname)
# print("Rootname: "+ rootname)
# print("Cleared: "+cleared)
os.rename(rootname, cleared+desc)
print("Successfully renamed dir to", cleared+desc)
except Exception as err:
logging.error(err)