[Python-Dev] Function in os module for available disk space, why
not?
M.-A. Lemburg
mal@lemburg.com
Mon, 19 Mar 2001 17:07:10 +0100
Fredrik Lundh wrote:
>
> dinu wrote:
> > Well, this is the usual "If you need it, do it yourself!"
> > answer, that bites the one who dares to speak up for all
> > those hundreds who don't... isn't it?
>
> fwiw, Python already supports this for real Unix platforms:
>
> >>> os.statvfs("/")
> (8192, 512, 524288, 365788, 348336, 600556, 598516, 598516, 0, 255)
>
> here, the root disk holds 524288x512 bytes, with 348336x512
> bytes free for the current user, and 365788x512 bytes available
> for root.
>
> (the statvfs module contains indices for accessing this "struct")
>
> Implementing a small subset of statvfs for Windows wouldn't
> be that hard (possibly returning None for fields that don't make
> sense, or are too hard to figure out).
>
> (and with win32all, I'm sure it can be done without any C code).
It seems that all we need is Jack to port this to the Mac
and we have a working API here :-)
Let's do it...
import sys,os
try:
os.statvfs
except KeyError:
# Win32 implementation...
# Mac implementation...
pass
else:
import statvfs
def freespace(path):
""" freespace(path) -> integer
Return the number of bytes available to the user on the file system
pointed to by path."""
s = os.statvfs(path)
return s[statvfs.F_BAVAIL] * long(s[statvfs.F_BSIZE])
if __name__=='__main__':
path = sys.argv[1]
print 'Free space on %s: %i kB (%i bytes)' % (path,
freespace(path) / 1024,
freespace(path))
--
Marc-Andre Lemburg
______________________________________________________________________
Company & Consulting: http://www.egenix.com/
Python Pages: http://www.lemburg.com/python/