-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
95886b8
commit 2d64991
Showing
1 changed file
with
48 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
#!/usr/bin/env python3 | ||
# -*- coding: utf-8 -*- | ||
|
||
# script by Ruchir Chawdhry | ||
# released under MIT License | ||
# github.com/RuchirChawdhry/Python | ||
# ruchirchawdhry.com | ||
# linkedin.com/in/RuchirChawdhry | ||
|
||
from subprocess import run | ||
from prettytable import PrettyTable | ||
|
||
# PS: This only works on macOS & Linux. It will not work on Windows | ||
# unless you install GNU coreutils: | ||
# http://gnuwin32.sourceforge.net/packages/coreutils.htm | ||
|
||
|
||
def folder_size(path): | ||
size = run(["du", "-sk", path], capture_output=True, encoding="utf-8") | ||
return size | ||
|
||
|
||
def megabytes(size): | ||
mb = int(size) / 1024 | ||
return round(mb, 2) | ||
|
||
|
||
def gigabytes(size): | ||
gb = (int(size) / 1024) / 1024 | ||
return round(gb, 2) | ||
|
||
|
||
def table_print(data): | ||
t = PrettyTable() | ||
mb = megabytes(data[0]) | ||
gb = gigabytes(data[0]) | ||
t.field_names = ["Folder/Directory", "KB", "MB", "GB"] | ||
t.add_row([data[1], data[0], mb, gb]) | ||
print(t) | ||
|
||
|
||
if __name__ == "__main__": | ||
try: | ||
s = folder_size(input("PATH TO FOLDER/DIR: ")) | ||
s = str(s.stdout).split("\t") | ||
table_print(s) | ||
except ValueError: | ||
print("Please enter a valid PATH without quotes or any other characters") |