Python Forum
Convert file sizes: will this produce accurate results?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Convert file sizes: will this produce accurate results?
#1

Any suggestions on how to improve this script would be awesome.


def convert_size(size_bytes): 
    if size_bytes == 0: 
        return "0B" 
    size_name = ("B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB") 
    i = int(math.floor(math.log(size_bytes, 1024)))
    power = math.pow(1024, i) 
    size = round(size_bytes / power, 2) 
    return "%s %s" % (size, size_name[i])
Reply
#2
Your function looks nice.

There is no much space for improvement.
You can use the newer string formatting syntax.

def convert_size(size_bytes): 
    if size_bytes == 0: 
        return "0B" 
    size_name = ("B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB") 
    i = int(math.floor(math.log(size_bytes, 1024)))
    power = math.pow(1024, i) 
    size = round(size_bytes / power, 2) 
    return "{} {}".format(size, size_name[i])
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#3
RickyWilson Wrote:will this produce accurate results?
You can write a test,pytest is advisable.
Here also with a couple of 3.6 features,f-string and underscore numbers.
Added doc-string then help() will work.
import math
import pytest

@pytest.mark.parametrize(
    'size_bytes, expected', [
        (1024 , '1.0 KB'),
        (1_048_576, '1.0 MB'),
        (1_099_511_627_776, '1.0 TB'),
    ]
)

def test_convert_size(size_bytes, expected):
    '''Convert Bytes to Kilobytes,Megabytes,Gigabytes,Terabytes...'''
    if size_bytes == 0:
        return "0B"
    size_name = ("B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB")
    i = int(math.floor(math.log(size_bytes, 1024)))
    power = math.pow(1024, i)
    size = round(size_bytes / power, 2)
    assert f"{size} {size_name}" == expected
pytest will find function named test and run values in mark.parametrize()
Output:
E:\1py_div\dec λ pytest ============================= test session starts ============================= platform win32 -- Python 3.6.2, pytest-3.3.0, py-1.5.2, pluggy-0.6.0 rootdir: E:\1py_div\dec, inifile: collected 3 items byte_convert_test.py ...                                                 [100%] ========================== 3 passed in 0.12 seconds ===========================
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question [SOLVED] Correct way to convert file from cp-1252 to utf-8? Winfried 8 547 Feb-29-2024, 12:30 AM
Last Post: Winfried
  Produce One file Per PurchaseOrder jland47 1 293 Jan-26-2024, 11:38 AM
Last Post: Larz60+
  How to create a table with different sizes of columns in MS word pepe 8 1,417 Dec-08-2023, 07:31 PM
Last Post: Pedroski55
  Updating sharepoint excel file odd results cubangt 1 756 Nov-03-2023, 05:13 PM
Last Post: noisefloor
  Convert File to Data URL michaelnicol 3 1,083 Jul-08-2023, 11:35 AM
Last Post: DeaD_EyE
  Python Script to convert Json to CSV file chvsnarayana 8 2,347 Apr-26-2023, 10:31 PM
Last Post: DeaD_EyE
  Printing effect sizes for variables in an anova eyavuz21 2 940 Feb-01-2023, 02:12 PM
Last Post: eyavuz21
  Trying to send file to printer with no results. chob_thomas 2 3,268 Dec-21-2022, 07:12 AM
Last Post: Pedroski55
  Convert Excel file into csv with Pipe symbol.. mg24 4 1,290 Oct-18-2022, 02:59 PM
Last Post: Larz60+
  Writing string to file results in one character per line RB76SFJPsJJDu3bMnwYM 4 1,309 Sep-27-2022, 01:38 PM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020