Implement file size output based on Python

Source: Internet
Author: User
When stored in a database, the use of Bytes is more accurate, scalable, and highly flexible.

When you output, you need to do some matching.

1. Precautions and Test code

1. You need to consider the scenario where sizeInBytes is None.

2. Divide by 1024.0 rather than 1024 to avoid loss of precision.

The implemented function is GETSIZEINMB (sizeinbytes), and the generic test code is

2. Output in MB--return float

In general, the size of the ebook between 1-50MB, the output of a unified conversion to MB is a good choice.

Disadvantages:

1. Output accuracy is too high, such as 10240000 Bytes calculation results of 10240000-9.765625

2. File size is limited, less than 1 MB or G-level data is not suitable for this way display

Advantage:

1. Suitable for participating in the calculation with return value

3. Keep 1 decimal places in megabytes--return str

With precision in mind, you can choose to retain 1 decimal places.

def GETSIZEINMB (sizeinbytes):

Return '%.1f '% ((sizeinbytes or 0)/(1024.0*1024.0),) # Use 1-dimension tuple is suggested

The return value is suggested as '%.1f '% (number,) rather than '%.1f '%

Both can be executed correctly, but the latter is easily misjudged as a function that executes only one parameter number, leading to difficult-to-judge errors.

3. Keep up to 1 decimal places in megabytes--return str

Most operating systems typically display up to 1 decimal places

4. Automatically select the best unit

Algorithm Description:

1. From the English grammar point of view, only 1 uses the singular form. The other 0/decimals are in plural form. Bytes level involved

2. In terms of accuracy, KB and above, retain 1 decimal places. Bytes reserves up to 1 decimal places.

This processing rule is not suitable for cases where decimals are very bit 0, such as 10.0 bytes,10.01 bytes. The input results are all bytes.

In other cases, there is no problem with precision.

Test data and results such as

The above content to introduce to you based on Python implementation of file size output of the relevant knowledge, I hope this article to share to help.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.