Skip to content

Commit 76ed4e5

Browse files
Merge pull request #1749 from prernamittal/diskspaceanalyzer
added disk space analyzer script
2 parents 88757ed + 3f1b445 commit 76ed4e5

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import os
2+
3+
def get_size(path):
4+
total_size = 0
5+
if os.path.isfile(path):
6+
return os.path.getsize(path)
7+
elif os.path.isdir(path):
8+
for dirpath, dirnames, filenames in os.walk(path):
9+
for f in filenames:
10+
fp = os.path.join(dirpath, f)
11+
total_size += os.path.getsize(fp)
12+
return total_size
13+
14+
def analyze_disk_space(directory):
15+
files = []
16+
folders = []
17+
for entry in os.scandir(directory):
18+
if entry.is_file():
19+
size = get_size(entry.path)
20+
files.append((entry.name, size))
21+
elif entry.is_dir():
22+
size = get_size(entry.path)
23+
folders.append((entry.name, size))
24+
25+
sorted_files = sorted(files, key=lambda x: x[1], reverse=True)
26+
sorted_folders = sorted(folders, key=lambda x: x[1], reverse=True)
27+
28+
print("Files:")
29+
for file in sorted_files:
30+
name, size = file
31+
print(f"{name}: {size} bytes")
32+
33+
print("\nFolders:")
34+
for folder in sorted_folders:
35+
name, size = folder
36+
print(f"{name}: {size} bytes")
37+
38+
# Specify the directory to analyze
39+
directory = r"C:\Users\python" #insert your directory path here
40+
41+
# Analyze the disk space
42+
analyze_disk_space(directory)

Disk Space Analyzer/readme.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Disk Space Analyzer
2+
3+
## Input
4+
<li>edit the directory path of a folder you want to analyze, defined by the 'directory' variable in the file.
5+
6+
## Output
7+
<li>list of all the sub folders and files with their size (in bytes) will be printed
8+
9+
## Author(s)
10+
[Prerna Mittal](https://github.com/prernamittal)

0 commit comments

Comments
 (0)