[Python-Dev] Function in os module for available disk space, why not?

M.-A. Lemburg mal@lemburg.com
Tue, 20 Mar 2001 22:03:40 +0100


Fredrik Lundh wrote:
> 
> Tim Peters wrote:
> > One of the best things Python ever did was to introduce os.path.getsize() +
> > friends, saving the bulk of the world from needing to wrestle with the
> > obscure Unix stat() API.
> 
> yup (I remember lobbying for those years ago), but that doesn't
> mean that we cannot make already existing low-level APIs work
> on as many platforms as possible...
> 
> (just like os.popen etc)
> 
> adding os.statvfs for windows is pretty much a bug fix (for 2.1?),
> but adding a new API is not (2.2).
> 
> > os.chmod() is another x-platform teachability pain
> 
> shutil.chmod("file", "g+x"), anyone?

Wasn't shutil declared obsolete ?
 
> > if there's anything worth knowing in the bowels of statvfs(), let's
> > please spell it in a human-friendly way from the start.
> 
> how about os.path.getfreespace("path") and
> os.path.gettotalspace("path") ?

Anybody care to add the missing parts in:

import sys,os

try:
    os.statvfs

except AttributeError:
    # 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))


totalspace() should be just as easy to add and I'm pretty
sure that you can get that information on *all* platforms
(not necessarily using the same APIs though).

-- 
Marc-Andre Lemburg
______________________________________________________________________
Company & Consulting:                           http://www.egenix.com/
Python Pages:                           http://www.lemburg.com/python/