File Attributes conversion

Tim Roberts timr at probo.com
Wed Mar 6 02:33:13 EST 2002


sam_collett at lycos.co.uk (Sam Collett) wrote:

>I can get file attributes using os.path.getsize and os.path.getmtime
>etc, but I am not sure how to convert them into something more
>understandable
>
>e.g.
>File Size to display in bytes (if < 3kb), kilobytes (if < 2MB) and
>megabytes (> 2MB). How would you do that (the value I get is 12345L -
>what is the L?)

Do it the hard way:

  def ShowSize(raw):
    if raw < 3000:
      return "%d" % raw
    elif raw < 2000000:
      return "%dk" % (raw / 1000)
    elif raw < 2000000000L:
      return "%dM" % (raw / 1000000)
    else:
      return "%dG" % (raw / 1000000000L)

Actually, I usually use a few more cases so that I always get two
significant digits:  1.2k, 23k, 840k, 2.4M, etc.
--
- Tim Roberts, timr at probo.com
  Providenza & Boekelheide, Inc.



More information about the Python-list mailing list