This forum is in archive mode. You will not be able to post new content.

Author Topic: [Python] calculate content size from bytes to something else  (Read 1726 times)

0 Members and 1 Guest are viewing this topic.

Offline Kulverstukas

  • Administrator
  • Zeus
  • *
  • Posts: 6627
  • Cookies: 542
  • Fascist dictator
    • View Profile
    • My blog
[Python] calculate content size from bytes to something else
« on: October 22, 2012, 07:13:50 PM »
Code: (python) [Select]
def calculateSize(self, bytes):
    abbrevs = ["kB", "mB", "gB"]
    if (bytes == None):
        size = "0 kB"
    else:
        bytes = float(bytes)
        for abbrev in abbrevs:
            if (bytes > 1024.0):
                bytes = bytes / 1024.0
                size = "%.2f %s" % (bytes, abbrev)
    return size

This is a simple script. It will divide the given bytes by 1024 until they are not more than 1024 or the last abbreviation is reached. To keep track of the current data size, a abbreviation is added at the end. If you want to add more of those, put them in the "abbrevs" list.

Example usage:
Code: (python) [Select]
print calculateSize(104857606)
will produce:
Code: [Select]
C:\>python test.py
100.00 mB
« Last Edit: October 22, 2012, 07:14:20 PM by Kulverstukas »

Offline Deque

  • P.I.N.N.
  • Global Moderator
  • Overlord
  • *
  • Posts: 1203
  • Cookies: 518
  • Programmer, Malware Analyst
    • View Profile
Re: [Python] calculate content size from bytes to something else
« Reply #1 on: October 22, 2012, 08:11:38 PM »
Nice snippet. Using this snippet in combination with os.path.getsize(filepath) will get you a better filesize output.

Offline s3my0n

  • Knight
  • **
  • Posts: 276
  • Cookies: 58
    • View Profile
    • ::1
Re: [Python] calculate content size from bytes to something else
« Reply #2 on: October 23, 2012, 01:22:50 AM »
Nice snippet. Using this snippet in combination with os.path.getsize(filepath) will get you a better filesize output.

You can also do "ls -lh" to give a better file size output. You can even do this to make your life easier: "alias ll="ls -lh " . Then you can just use "ll" to view files with human readable file sizes.
Easter egg in all *nix systems: E(){ E|E& };E

Offline Kulverstukas

  • Administrator
  • Zeus
  • *
  • Posts: 6627
  • Cookies: 542
  • Fascist dictator
    • View Profile
    • My blog
Re: [Python] calculate content size from bytes to something else
« Reply #3 on: October 23, 2012, 09:12:52 AM »
You can also do "ls -lh" to give a better file size output. You can even do this to make your life easier: "alias ll="ls -lh " . Then you can just use "ll" to view files with human readable file sizes.
But that's not python anymore :P

Offline madara

  • Serf
  • *
  • Posts: 23
  • Cookies: 3
    • View Profile
Re: [Python] calculate content size from bytes to something else
« Reply #4 on: June 18, 2013, 12:12:12 PM »
Code: (python) [Select]
def calculateSize(self, bytes):
    abbrevs = ["kB", "mB", "gB"]
    if (bytes == None):
        size = "0 kB"
    else:
        bytes = float(bytes)
        for abbrev in abbrevs:
            if (bytes > 1024.0):
                bytes = bytes / 1024.0
                size = "%.2f %s" % (bytes, abbrev)
    return size



ok can be good, but now we can rewrite it in python:


Code: (python) [Select]

import math
def byteszConverter(bytes=1):
    abbrevs = ['bytes', 'kb', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']
    esp = math.floor(math.log(bytes)/math.log(1024))
    return '{0:.2f}'.format(bytes/math.pow(1024, esp)) + ' ' + abbrevs[esp]
Python 2.x users  need simple changes...
« Last Edit: June 18, 2013, 12:20:22 PM by madara »

 



Want to be here? Contact Ande, Factionwars or Kulverstukas on the forum or at IRC.