- Notifications
You must be signed in to change notification settings - Fork 638
Expand file tree
/
Copy pathP03_GetFileSize.py
More file actions
Latest commit
18 lines (14 loc) · 1.27 KB
/
Copy pathP03_GetFileSize.py
File metadata and controls
18 lines (14 loc) · 1.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# Author : Craig Richards
# Description : This will scan the current directory and all subdirectories and display the size.
importos# Load the library module
directory='/home/omkarpathak/Documents/GITs/Python-Programs/Scripts'# Set the variable directory to be the current directory
dir_size=0# Set the size to 0
fsizedicr= {'Bytes': 1, 'Kilobytes': float(1)/1024, 'Megabytes': float(1)/(1024*1024), 'Gigabytes': float(1)/(1024*1024
*
1024)}
for (path, dirs, files) inos.walk(directory): # Walk through all the directories. For each iteration, os.walk returns the folders, subfolders and files in the dir.
forfileinfiles: # Get all the files
filename=os.path.join(path, file)
dir_size+=os.path.getsize(filename) # Add the size of each file in the root dir to get the total size.
forkeyinfsizedicr: #iterating through the dictionary
print ("Folder Size: "+str(round(fsizedicr[key]*dir_size, 2)) +" "+key) # round function example: round(4.2384, 2) ==> 4.23