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.

Author davidsarah
Recipients carstenkoch, davidsarah, effbot, gvanrossum, tim.peters
Date 2011-01-09.19:54:13
SpamBayes Score 4.4804027e-09
Marked as misclassified No
Message-id <1294602856.15.0.284617060798.issue410547@psf.upfronthosting.co.za>
In-reply-to
Content
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
2011-01-09 19:54:16davidsarahsetrecipients: + davidsarah, gvanrossum, tim.peters, effbot, carstenkoch
2011-01-09 19:54:16davidsarahsetmessageid: <1294602856.15.0.284617060798.issue410547@psf.upfronthosting.co.za>
2011-01-09 19:54:13davidsarahlinkissue410547 messages
2011-01-09 19:54:13davidsarahcreate