-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsize.py
More file actions
41 lines (35 loc) · 1.21 KB
/
size.py
File metadata and controls
41 lines (35 loc) · 1.21 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
import os
import sys
import site
def get_package_size(path):
total_size = 0
for dirpath, _, filenames in os.walk(path):
for f in filenames:
try:
fp = os.path.join(dirpath, f)
if os.path.isfile(fp):
total_size += os.path.getsize(fp)
except OSError:
pass
return total_size
def main():
# Find the site-packages directory
site_packages_dirs = site.getsitepackages() if hasattr(site, "getsitepackages") else [site.getusersitepackages()]
package_sizes = []
for site_dir in site_packages_dirs:
if not os.path.isdir(site_dir):
continue
for item in os.listdir(site_dir):
item_path = os.path.join(site_dir, item)
if os.path.isdir(item_path):
size_mb = get_package_size(item_path) / (1024 * 1024)
package_sizes.append((item, size_mb))
# Sort from largest to smallest
package_sizes.sort(key=lambda x: x[1], reverse=True)
# Print results
print(f"{'Package':<40} {'Size (MB)':>10}")
print("-" * 52)
for name, size in package_sizes:
print(f"{name:<40} {size:>10.2f}")
if __name__ == "__main__":
main()