-
Notifications
You must be signed in to change notification settings - Fork 0
/
file_walker.py
31 lines (27 loc) · 1.03 KB
/
file_walker.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
import os
from pprint import pprint
def my_dir_walker_with_size_counting(topdir=None):
if topdir is None:
topdir = os.getcwd()
sizes = {topdir: 0} # sciezka: rozmiar w bajtach
stack = []
def inner_walker(new_topdir):
stack.append(new_topdir)
new_topdir_size = os.path.join(*stack) # wszystko bedzie rozpakowane do stacka
#TODO: dodać obłsugę błedów
entries = os.scandir(new_topdir)
size = 0
for entry in entries:
if entry.is_dir(follow_symlinks=False):
entry_size = inner_walker(entry.name)
sizes[os.path.join(*stack)] = entry_size
size += entry_size
stack.pop()
else:
fpath = os.path.join(*stack, entry.name)
sizes[fpath] = os.path.getsize(fpath)
size += os.path.getsize(fpath)
return size
inner_walker(topdir)
return sizes
pprint(my_dir_walker_with_size_counting('./Users/negativeone')) # bardziej sprecyzować sciezke