Source

woocode / py / size_tool.py

def get_size(s):
    _u = 1024
    KB = _u
    MB = _u ** 2
    GB = _u ** 3

    size_map = {
        'kb': KB,
        'mb': MB,
        'gb': GB}
    s = s.lower().strip()
    size = 0.0
    for k, v in size_map.iteritems():
        if k in s:
            s = s.strip(k).strip()
            try:
                size = float(s) * v
            except ValueError:
                print 'error string', s
                break
    size = int(size)
    return size


print get_size('100 MB')
print get_size('20 KB')
print get_size('1 GB')