python 根据字节计算文件大小(k/m/g)的函数

By admin at 2013-12-08 11:54:44 • 1315次点击

根据字节计算文件的大小

def str_filesize(size):
    """
    author: limodou
    >>> print str_filesize(0)
    0
    >>> print str_filesize(1023) 
    1023
    >>> print str_filesize(1024)
    1K
    >>> print str_filesize(1024*2)
    2K
    >>> print str_filesize(1024**2-1)
    1023K
    >>> print str_filesize(1024**2)
    1M
    """
    import bisect
    d = [(1024-1,'K'), (1024**2-1,'M'), (1024**3-1,'G'), (1024**4-1,'T')]
    s = [x[0] for x in d]
    index = bisect.bisect_left(s, size) - 1
    if index == -1:
        return str(size)
    else:
        b, u = d[index]
    return str(size / (b+1)) + u

python, 字节, 文件, 函数


测试代码

$site_infos = get_site_infos();
$newest_nodes = get_newest_nodes();
if(count($newest_nodes)==$options['newest_node_num']){
    $bot_nodes = get_bot_nodes();
}

$show_sider_ad = "1";
$links = get_links();

沐雨 at 2014-10-30 23:53:30
1

不知道楼主是在用youbbs 吗

q26486328 at 2014-10-30 23:56:38
2
登录 后发表评论