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
  Convert legacy print file to XLSX file davidm 1 332 Oct-17-2021, 05:08 AM
Last Post: davidm
  How to save some results in .txt file with Python? Melcu54 4 1,953 May-26-2021, 08:15 AM
Last Post: snippsat
  Yahoo_fin, Pandas: how to convert data table structure in csv file detlefschmitt 14 3,108 Feb-15-2021, 12:58 PM
Last Post: detlefschmitt
  Running A Parser In VSCode - And Write The Results Into A Csv-File apollo 5 1,653 Jan-14-2021, 08:58 PM
Last Post: snippsat
  [split] How to convert the CSV text file into a txt file Pinto94 5 1,244 Dec-23-2020, 08:04 AM
Last Post: ndc85430
  Writing unit test results into a text file ateestructural 3 1,315 Nov-15-2020, 05:41 PM
Last Post: ateestructural
  CPC File Format (Cartesian Perceptual Compression) - Can Python Convert / Handle Them PSKrieger 2 932 Nov-11-2020, 02:57 PM
Last Post: PSKrieger
  capture pytest results to a file maiya 2 1,423 Oct-17-2020, 03:42 AM
Last Post: maiya
  Convert file of hex strings to binary file medatib531 4 3,371 Oct-09-2020, 05:42 PM
Last Post: DeaD_EyE
  How to convert what appears to be a JSON file to CSV NewBeie 4 1,121 Aug-28-2020, 04:45 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

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