File tree Expand file tree Collapse file tree 2 files changed +52
-0
lines changed Expand file tree Collapse file tree 2 files changed +52
-0
lines changed Original file line number Diff line number Diff line change
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 ("\n Folders:" )
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 )
Original file line number Diff line number Diff line change
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 )
You can’t perform that action at this time.
0 commit comments