From a94db8a5ce9a4df32645192f03254ec3e1c10ce0 Mon Sep 17 00:00:00 2001 From: Gavin D'souza Date: Mon, 28 Sep 2020 19:13:31 +0530 Subject: [PATCH] fix: Added human readable file size func --- frappe/utils/__init__.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/frappe/utils/__init__.py b/frappe/utils/__init__.py index a32a98cde5..d3bf1dd10c 100644 --- a/frappe/utils/__init__.py +++ b/frappe/utils/__init__.py @@ -706,3 +706,18 @@ def get_html_for_route(route): response = render.render() html = frappe.safe_decode(response.get_data()) return html + +def get_file_size(path, format=False): + num = os.path.getsize(path) + + if not format: + return num + + suffix = 'B' + + for unit in ['','Ki','Mi','Gi','Ti','Pi','Ei','Zi']: + if abs(num) < 1024: + return "{0:3.1f}{1}{2}".format(num, unit, suffix) + num /= 1024 + + return "{0:.1f}{1}{2}".format(num, 'Yi', suffix)