linux disc space

Jeff Schwab jeff at schwabcenter.com
Fri Feb 15 15:03:00 EST 2008


DataSmash wrote:
> On Feb 15, 1:32 pm, Jeff Schwab <j... at schwabcenter.com> wrote:
>> Chris wrote:
>>> On Feb 15, 7:10 pm, DataSmash <r... at new.rr.com> wrote:
>>>> I simply want to capture the free disc space in a variable so that I
>>>> can compare changes.  I'm aware of a few commands like "df -h" or "du -
>>>> k", but I can't figure out how to capture those values as a variable.
>>>> I also looked at os.statvfs(), but that output doesn't seem to make
>>>> any sense at all to me, knowing the size of the disc.
>>>> Thanks for your help!
>>>> R.D.
>>> import os, statvfs
>>> s = os.statvfs(".")
>>> freebytes = s[statvfs.F_BSIZE] * s[statvfs.F_BAVAIL]
>> Is it worth distinguishing free bytes from available bytes?  I've never
>> seen them differ, and I'm not sure how they ever would...
>>
>>      import os
>>      import statvfs
>>
>>      def free_bytes(path):
>>          stats = os.statvfs(path)
>>          return stats[statvfs.F_BSIZE] * stats[statvfs.F_BFREE]
>>
>>      def avail_bytes(path):
>>          stats = os.statvfs(path)
>>          return stats[statvfs.F_BSIZE] * stats[statvfs.F_BAVAIL]
>>
>>      if __name__ == '__main__':
>>          import sys
>>          for path in sys.argv[1:]:
>>              print "%s:" % path,
>>              print "%dK free," % (free_bytes(path) / 1024),
>>              print "%dK available" % (avail_bytes(path) / 1024)
> 
> 
> Chris,
> Much thanks.  That's just what I need.
> 
> Jeff,
> Not sure what's taking up the "available" space, but it's about 12GB
> on my system.
> Interesting.

Available space is how much you can actually access as a non-root user. 
  Apparently (thank you Jean-Paul), space can be reserved for superuser 
use only; such space is "free," but not "available."

I'm not sure how superuser-only space would be reserved in the first 
place.  I don't see anything relevant in the fdisk man page.



More information about the Python-list mailing list