Forum: Poser Python Scripting


Subject: Useful Code Snippets

structure opened this issue on Jun 27, 2019 ยท 94 posts


structure posted Sun, 20 March 2022 at 8:30 PM Forum Coordinator

Convert bytes to Kb, Mb, Gb, or Tb

def formatSize(bytes):
    try:
        bytes = float(bytes)
        kb = bytes / 1024
    except:
        return "Error"
    if kb >= 1024:
        M = kb / 1024
        if M >= 1024:
            G = M / 1024
            if G >= 1024:
                T = G / 1024
                return "%.2f Tb" % (T)
            return "%.2f Gb" % (G)
        return "%.2f Mb" % (M)
    else:
        return "%.2f kb" % (kb)

Example Output:




Locked Out