-
Notifications
You must be signed in to change notification settings - Fork 4
/
dirsize.py
executable file
·99 lines (81 loc) · 2.9 KB
/
dirsize.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#!/usr/bin/env python
########################################################################
# dirsize.py: Directory Size Summary Script
#
# Description:
# This script displays a detailed list of files and directories in the
# specified directory (or the current directory if none is specified),
# including hidden files, and calculates the total size of these files
# in human-readable format (KiB, MiB, GiB, TiB, PiB, EiB, ZiB, YiB, RiB, QiB).
# It's useful for quickly estimating the size of contents in a directory
# without including subdirectories. The script does not recursively account
# for sizes within subdirectories and is designed for simplicity, providing
# a clear overview of the immediate directory contents.
#
# Author: id774 (More info: http://id774.net)
# Source Code: https://github.com/id774/scripts
# License: LGPLv3 (Details: https://www.gnu.org/licenses/lgpl-3.0.html)
# Contact: [email protected]
#
# Version History:
# v1.1 2024-08-07
# Added -h option to display help. Modified script to show help when no directory is specified.
# v1.0 2023-12-19
# Initial release. Displays directory contents and calculates total size
# using binary prefixes.
#
# Usage:
# ./dirsize.py [directory]
# If no directory is specified, the current directory is used.
#
########################################################################
import os
import subprocess
import sys
from decimal import Decimal, getcontext
def convert_size(size_bytes):
if size_bytes == 0:
return "0B"
i = 0
prefix = ["B", "KiB", "MiB", "GiB", "TiB",
"PiB", "EiB", "ZiB", "YiB", "RiB", "QiB"]
size_bytes = Decimal(size_bytes)
getcontext().prec = 50
while size_bytes >= Decimal(1024) and i < len(prefix) - 1:
size_bytes /= Decimal(1024)
i += 1
return "{:.2f} {}".format(size_bytes, prefix[i])
def display_help():
print("""
Usage: ./dirsize.py [options] [directory]
Options:
-h Display this help message and exit
If no directory is specified, it displays this help message.
To check the current directory, use:
./dirsize.py .
""")
def main():
if len(sys.argv) == 1:
display_help()
sys.exit(0)
elif sys.argv[1] == '-h':
display_help()
sys.exit(0)
directory = sys.argv[1]
# Check if the directory exists
if not os.path.exists(directory):
print("Error: Directory '{}' does not exist.".format(directory))
sys.exit(1)
total_size = 0
try:
# Run ls -all command and print output
subprocess.call(["ls", "-all", directory])
# Calculate total size of files in directory
for entry in os.scandir(directory):
if entry.is_file():
total_size += entry.stat().st_size
print("Total size:", convert_size(total_size))
except Exception as e:
print("Error:", e)
if __name__ == "__main__":
main()