How to format bytes to human readable Size in Python

To format bytes to human readable Size in Python you can use the following snippet.

Sample Python

fileSize = 12454162221
for count in ['Bytes','KB','MB','GB']:
    if fileSize > -1024.0 and fileSize < 1024.0:
        print "%3.1f%s" % (fileSize, count)
    fileSize /= 1024.0
print "%3.1f%s" % (fileSize, 'TB')

One thought on “How to format bytes to human readable Size in Python

Leave a Reply