Skip to content
This repository was archived by the owner on Dec 22, 2023. It is now read-only.

Commit 9914a74

Browse files
Added python code and Readme
1 parent 90e89ef commit 9914a74

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
Disk Usage Stats
2+
3+
A python script which takes a command line argument as a directory (default specified) and stores the stats in a csv file.
4+
5+
To run the script:
6+
7+
In Linux:
8+
./disk_usage.py /home
9+
10+
In Windows:
11+
python3 disk_usage.py
12+
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#!/usr/bin/python3
2+
3+
import sys
4+
import pandas as pd
5+
import os
6+
7+
def get_size(path):
8+
9+
total = 0
10+
for entry in os.scandir(path):
11+
try:
12+
if entry.is_dir(follow_symlinks = False):
13+
total += get_size(entry.path)
14+
15+
else:
16+
total += entry.stat(follow_symlinks = False).st_size
17+
18+
except Exception as e:
19+
print("Exception: ", e)
20+
total += 0
21+
22+
return total
23+
24+
if __name__ == '__main__':
25+
26+
# if you're running this in linux, change the default path to "/home"
27+
# also run the code as "sudo python3 Disk_usage.py" or "sudo ./Disk_usage.py"
28+
# i.e, with super user permissions
29+
30+
path = "C:/Users/"
31+
print("Total arguments passed: ", len(sys.argv))
32+
33+
directory = sys.argv[1] if len(sys.argv) >= 2 else path
34+
usage = []
35+
paths = []
36+
37+
for entry in os.scandir(directory):
38+
if entry.is_dir(follow_symlinks = False):
39+
40+
print(entry.path + " is a directory.")
41+
print(get_size(entry.path))
42+
total = get_size(entry.path)
43+
paths.append(entry.path)
44+
usage.append(total)
45+
46+
usage_dict = {"directory" : paths, "usage" : usage}
47+
df = pd.DataFrame(usage_dict)
48+
49+
print(df)
50+
df.to_csv("disk_home_usage.csv")

0 commit comments

Comments
 (0)