Skip to content

Commit ff5be6b

Browse files
committed
Python Script for getting file size
1 parent b0ee3ed commit ff5be6b

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

Scripts/P03_GetFileSize.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Author : Craig Richards
2+
# Description : This will scan the current directory and all subdirectories and display the size.
3+
4+
import os # Load the library module
5+
directory = '/home/omkarpathak/Documents/GITs/Python-Programs/Scripts' # Set the variable directory to be the current directory
6+
dir_size = 0 # Set the size to 0
7+
8+
fsizedicr = {'Bytes': 1, 'Kilobytes': float(1)/1024, 'Megabytes': float(1)/(1024*1024), 'Gigabytes': float(1)/(1024*1024
9+
*
10+
1024)}
11+
12+
for (path, dirs, files) in os.walk(directory): # Walk through all the directories. For each iteration, os.walk returns the folders, subfolders and files in the dir.
13+
for file in files: # Get all the files
14+
filename = os.path.join(path, file)
15+
dir_size += os.path.getsize(filename) # Add the size of each file in the root dir to get the total size.
16+
17+
for key in fsizedicr: #iterating through the dictionary
18+
print ("Folder Size: " + str(round(fsizedicr[key]*dir_size, 2)) + " " + key) # round function example: round(4.2384, 2) ==> 4.23

0 commit comments

Comments
 (0)