python 文件大小显示kb、mb或gb等
2014-10-29 10:55
1071 查看
SUFFIXES
=
{1000:
['KB',
'MB',
'GB',
'TB',
'PB',
'EB',
'ZB',
'YB'],
1024:
['KiB',
'MiB',
'GiB',
'TiB',
'PiB',
'EiB',
'ZiB',
'YiB']}
def
approximate_size(size,
a_kilobyte_is_1024_bytes=True):
'''Convert a file size
to human-readable form.
Keyword arguments:
size -- file size in bytes
a_kilobyte_is_1024_bytes -- if True (default), use multiples of 1024
if False, use multiples of 1000
Returns: string
'''
if
size <
0:
raise
ValueError('number
must be non-negative')
multiple =
1024
if
a_kilobyte_is_1024_bytes else
1000
for
suffix in
SUFFIXES[multiple]:
size /=
multiple
if
size <
multiple:
return
'{0:.1f} {1}'.format(size,
suffix)
raise
ValueError('number
too large')
=
{1000:
['KB',
'MB',
'GB',
'TB',
'PB',
'EB',
'ZB',
'YB'],
1024:
['KiB',
'MiB',
'GiB',
'TiB',
'PiB',
'EiB',
'ZiB',
'YiB']}
def
approximate_size(size,
a_kilobyte_is_1024_bytes=True):
'''Convert a file size
to human-readable form.
Keyword arguments:
size -- file size in bytes
a_kilobyte_is_1024_bytes -- if True (default), use multiples of 1024
if False, use multiples of 1000
Returns: string
'''
if
size <
0:
raise
ValueError('number
must be non-negative')
multiple =
1024
if
a_kilobyte_is_1024_bytes else
1000
for
suffix in
SUFFIXES[multiple]:
size /=
multiple
if
size <
multiple:
return
'{0:.1f} {1}'.format(size,
suffix)
raise
ValueError('number
too large')
相关文章推荐
- 文件大小转换成可显示的Mb,Gb和kb方法
- PHP获取远程文件大小 如果加判断文件大小以KB MB GB
- 文件大小long转KB,MB,GB
- C#转换文件大小格式(GB/MB/KB/B)
- 文件大小单位实现,B,KB,MB,GB,TB.....
- Android 技术总结(014)—— 获取与转转文件的大小(B,KB,MB,GB)
- Android:根据文件大小自动转化为KB, MB, GB
- 获取app文件大小 并对应响应单位:B KB MB GB; DecimalFormat
- 取文件的大小 (KB,MB,GB...)
- php文件大小单位转换GB MB KB
- php文件大小单位转换GB MB KB
- 后台获取文件大小 自动转换 B KB MB GB
- Android 获取文件大小转换MB.GB.KB.B
- 取文件的大小 (KB,MB,GB...)
- vc显示数据大小(将十进制数据转化为Byte KB MB GB)
- 我的Android进阶之旅------>Java文件大小转换工具类 (B,KB,MB,GB,TB,PB之间的大小转换)
- 使用 B、KB、MB、GB 等常规格式显示文件大小
- 文件大小转换(b,kb,M,GB/TB)
- android 获取文件夹、文件的大小 以B、KB、MB、GB 为单位
- python 生成目录树及显示文件大小的代码