Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Filesize with nice units (See related posts)

Simple function to format filesizes in bytes to human-readable units.

def prettySize(size):
	suffixes = [("B",2**10), ("K",2**20), ("M",2**30), ("G",2**40), ("T",2**50)]
	for suf, lim in suffixes:
		if size > lim:
			continue
		else:
			return round(size/float(lim/2**10),2).__str__()+suf

print prettySize(213458923)
# Output: 203.57M

print prettySize(1234)
# Output: 1.21K

You need to create an account or log in to post comments to this site.


Click here to browse all 5147 code snippets

Related Posts