<a href="http://woodpecker.org.cn/diveintopython3/">http://woodpecker.org.cn/diveintopython3/</a>
![](https://img.laitimes.com/img/9ZDMuAjOiMmIsIjOiQnIsIyZwpmLyUDMlhTM0gjM1IGN0EzMhNzNmRDM4ImN1ImNiFjNlVTL1ATNyYTMxMzLcFDMzEDMy8CX4MDN1YDNvw1ZvxmYvwVbvNmLn9GbiRXauNmLzV2Zh1Wavw1LcpDc0RHaiojIsJye.jpg)
同時推薦如下連結--
<a href="http://linuxtoy.org/archives/9-free-python-books.html">http://linuxtoy.org/archives/9-free-python-books.html</a>
copy自己測試的第一個sample:
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-readeable 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')
if __name__ == '__main__':
print(approximate_size(100,false))
print(approximate_size(100))