This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: os.statvfs support for Windows
Type: Stage:
Components: Windows Versions: Python 3.3, Python 2.7
process
Status: closed Resolution: rejected
Dependencies: Superseder:
Assigned To: effbot Nosy List: carstenkoch, davidsarah, effbot, gvanrossum, tim.peters
Priority: low Keywords: patch

Created on 2001-03-22 16:58 by effbot, last changed 2022-04-10 16:03 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
statvfs-patch effbot, 2001-03-22 16:58 statvfs patch
Messages (10)
msg36183 - (view) Author: Fredrik Lundh (effbot) * (Python committer) Date: 2001-03-22 16:58
This patch adds (partial) os.statvfs support for
Windows.

The FRSIZE, BLOCKS, BFREE, BAVAIL, and NAMEMAX
fields are set.  Remaining fields are set to
zero, but should probably be set to None.

</F>
msg36184 - (view) Author: Tim Peters (tim.peters) * (Python committer) Date: 2001-03-22 21:35
Logged In: YES 
user_id=31435

/F, can you add a doc patch (for os.statvfs) that says 
enough so that a Windows user has some chance of guessing 
what this function returns?

BTW, I have no problem w/ returning zeroes instead of Nones.
msg36185 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2001-03-29 17:28
Logged In: YES 
user_id=6380

Tim, wasn't this supposed to go into 2.1b2?  Did it?  Can it
go into 2.1final?
msg36186 - (view) Author: Tim Peters (tim.peters) * (Python committer) Date: 2001-03-30 04:13
Logged In: YES 
user_id=31435

No, it did not go in.  I asked for a doc patch first so I 
wouldn't have to pee away time trying to guess what it 
does.  Now I've peed away the time, and I don't like it.  
Here's what it returns on my home desktop box (Win98SE, 
20Gb drive):

>>> os.statvfs("c:\\")  # argument is very touchy
(32768, 32768, 65526, 65526, 65526, 0, 0, 0, 0, 1024)
>>>

*Nothing* there makes sense, except for the last "max path" 
result.  Even the "block size" is wrong (the FAT32 fs on 
this box uses 16Kb clusters, not 32Kb).  Digging into the 
MS docs,

"""The GetDiskFreeSpace function returns incorrect values 
for volumes that are larger than 2 gigabytes"""

and

"""Even on volumes that are smaller than 2 gigabytes, the 
values stored into *lpSectorsPerCluster, 
*lpNumberOfFreeClusters, and *lpTotalNumberOfClusters 
values may be incorrect"""

under Win95, and, to judge from my desktop box, it appears 
useless under the latest flavor of Win98 too.

The function the patch uses is obsolete, and a ...Ex 
version is recommended in its place, which

"""returns correct values for all volumes, including those 
that are greater than 2 gigabytes"""

BUT, *that* function isn't available under the original 
Win95, only under Win95 OSR2 and later.  In addition, that 
function only returns number of bytes total and free (as 64 
bit unsigned ints), nothing about block size, # clusters, 
etc.  (OTOH, total bytes is free is what people asked for!  
I don't recall anyone asking for a statvfs() clone)

So bouncing back to you:  how much time do you want me to 
devote to this?
msg36187 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2001-03-30 12:22
Logged In: YES 
user_id=6380

Thanks for the investigation, Tim!

Bouncing it back to Fredrik and lowered priority.

I certainly don't want to spend a lot of time on it; I
believe that the flamewar that started this was ill-advised
anyway. Finding out free space is hard; too bad.
msg36188 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2001-12-10 15:51
Logged In: YES 
user_id=6380

Rejecting, since nobody seems interested in moving on this.
msg36189 - (view) Author: Carsten Koch (carstenkoch) Date: 2007-06-20 18:12
Well, this has been rejected 6 years ago...
Is there a portable way to get the available disk space by now?
msg125527 - (view) Author: David-Sarah Hopwood (davidsarah) Date: 2011-01-06 09:02
"Is there a portable way to get the available disk space by now?"

No, but http://tahoe-lafs.org/trac/tahoe-lafs/browser/src/allmydata/util/fileutil.py?rev=4894#L308 might be helpful (uses pywin32).
msg125844 - (view) Author: Carsten Koch (carstenkoch) Date: 2011-01-09 14:07
Here is what I am doing now:

...
if sys.platform == "win32":
   import win32file
else:
   import statvfs
...


def get_free_space(path = '.'):
   """
      Determine the free space in bytes for the given path.
   """
   if sys.platform == "win32":
      sizes = win32file.GetDiskFreeSpace(path)
      return sizes[0] * sizes[1] * sizes[2]
   else:
      status = os.statvfs(path)
      return status[statvfs.F_BAVAIL] * status[statvfs.F_FRSIZE]



def get_partition_size(path = '.'):
   """
      Determine the total space in bytes for the given path.
   """
   if sys.platform == "win32":
      sizes = win32file.GetDiskFreeSpace(path)
      return sizes[0] * sizes[1] * sizes[3]
   else:
      status = os.statvfs(path)
      return status[statvfs.F_BLOCKS] * status[statvfs.F_FRSIZE]
msg125854 - (view) Author: David-Sarah Hopwood (davidsarah) Date: 2011-01-09 19:54
Don't use win32file.GetDiskFreeSpace; the underlying Windows API only supports drives up to 2 GB (http://blogs.msdn.com/b/oldnewthing/archive/2007/11/01/5807020.aspx). Use GetFreeDiskSpaceEx, as the code I linked to does.

I'm not sure it makes sense to provide an exact clone of os.statvfs, since some of the statvfs fields don't have equivalents that are obtainable by any Windows API as far as I know. What <em>would</em> make sense is a cross-platform way to get total disk space, and the space free for root/Administrator and for the current user. This would actually be somewhat easier to use on Unix as well.

Anyway, here's some code for Windows that only uses ctypes (whichdir should be Unicode):

    from ctypes import WINFUNCTYPE, windll, POINTER, byref, c_ulonglong
    from ctypes.wintypes import BOOL, DWORD, LPCWSTR

    # <http://msdn.microsoft.com/en-us/library/aa383742%28v=VS.85%29.aspx>
    PULARGE_INTEGER = POINTER(c_ulonglong)

    # <http://msdn.microsoft.com/en-us/library/aa364937%28VS.85%29.aspx>
    GetDiskFreeSpaceExW = WINFUNCTYPE(BOOL, LPCWSTR, PULARGE_INTEGER, PULARGE_INTEGER, PULARGE_INTEGER)(
        ("GetDiskFreeSpaceExW", windll.kernel32))

    # <http://msdn.microsoft.com/en-us/library/ms679360%28v=VS.85%29.aspx>
    GetLastError = WINFUNCTYPE(DWORD)(("GetLastError", windll.kernel32))

    # (This might put up an error dialog unless
    # SetErrorMode(SEM_FAILCRITICALERRORS | SEM_NOOPENFILEERRORBOX)
    # has been called.)

    n_free_for_user = c_ulonglong(0)
    n_total         = c_ulonglong(0)
    n_free          = c_ulonglong(0)
    retval = GetDiskFreeSpaceExW(whichdir,
                                 byref(n_free_for_user),
                                 byref(n_total),
                                 byref(n_free))
    if retval == 0:
        raise OSError("Windows error %d attempting to get disk statistics for %r"
                      % (GetLastError(), whichdir))

    free_for_user = n_free_for_user.value
    total         = n_total.value
    free          = n_free.value
History
Date User Action Args
2022-04-10 16:03:53adminsetgithub: 34217
2011-01-09 19:54:13davidsarahsetnosy: gvanrossum, tim.peters, effbot, carstenkoch, davidsarah
messages: + msg125854
versions: + Python 2.7, Python 3.3, - Python 2.6
2011-01-09 14:07:47carstenkochsetnosy: gvanrossum, tim.peters, effbot, carstenkoch, davidsarah
messages: + msg125844
versions: + Python 2.6
2011-01-06 09:02:47davidsarahsetnosy: + davidsarah
messages: + msg125527
2001-03-22 16:58:15effbotcreate