-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathDisk_usage_stats.py
More file actions
50 lines (36 loc) · 1.31 KB
/
Disk_usage_stats.py
File metadata and controls
50 lines (36 loc) · 1.31 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
42
43
44
45
46
47
48
49
50
#!/usr/bin/python3
import sys
import pandas as pd
import os
def get_size(path):
total = 0
for entry in os.scandir(path):
try:
if entry.is_dir(follow_symlinks = False):
total += get_size(entry.path)
else:
total += entry.stat(follow_symlinks = False).st_size
except Exception as e:
print("Exception: ", e)
total += 0
return total
if __name__ == '__main__':
# if you're running this in linux, change the default path to "/home"
# also run the code as "sudo python3 Disk_usage.py" or "sudo ./Disk_usage.py"
# i.e, with super user permissions
path = "C:/Users/"
print("Total arguments passed: ", len(sys.argv))
directory = sys.argv[1] if len(sys.argv) >= 2 else path
usage = []
paths = []
for entry in os.scandir(directory):
if entry.is_dir(follow_symlinks = False):
print(entry.path + " is a directory.")
print(get_size(entry.path))
total = get_size(entry.path)
paths.append(entry.path)
usage.append(total)
usage_dict = {"directory" : paths, "usage" : usage}
df = pd.DataFrame(usage_dict)
print(df)
df.to_csv("disk_home_usage.csv")