ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/ns_dev/Python/NinoCode/Tool_Box/GetTreeSize.py
Revision: 635
Committed: Wed Mar 28 20:14:31 2018 UTC (8 years ago) by nino.borges
Content type: text/x-python
File size: 766 byte(s)
Log Message:
Initial version of a tree size getter that is new and better than the walk way

File Contents

# Content
1 """
2
3 GetTreeSize
4
5 Just testing the new scandir.ENOENT
6
7
8 """
9
10 import scandir
11
12 def get_tree_size(path):
13 """Return total size of files in given path and subdirs."""
14 total = 0
15 for entry in scandir.scandir(path):
16 if entry.is_dir(follow_symlinks=False):
17 total += get_tree_size(entry.path)
18 else:
19 total += entry.stat(follow_symlinks=False).st_size
20 return total
21
22
23 def human(size):
24
25 B = "B"
26 KB = "KB"
27 MB = "MB"
28 GB = "GB"
29 TB = "TB"
30 UNITS = [B, KB, MB, GB, TB]
31 HUMANFMT = "%f %s"
32 HUMANRADIX = 1024.
33
34 for u in UNITS[:-1]:
35 if size < HUMANRADIX : return HUMANFMT % (size, u)
36 size /= HUMANRADIX
37
38 return HUMANFMT % (size, UNITS[-1])