Skip to content
7 changes: 5 additions & 2 deletions Doc/library/shutil.rst
Original file line number Diff line number Diff line change
Expand Up @@ -343,11 +343,14 @@ Directory and files operations

Return disk usage statistics about the given path as a :term:`named tuple`
with the attributes *total*, *used* and *free*, which are the amount of
total, used and free space, in bytes. On Windows, *path* must be a
directory; on Unix, it can be a file or directory.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: I'd keep the "it can be a file or directory." part and add a versionchanged marker to explain that it now can accept file and directory on Windows. Example:

.. versionchanged:: 3.8
   Explain the behaivor change here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok - cool - I wasn't sure what the convention was. I'll update.

total, used and free space, in bytes. *path* may be a file or a
directory.

.. versionadded:: 3.3

.. versionchanged:: 3.8
On Windows, *path* can now be a file or directory.

Availability: Unix, Windows.

.. function:: chown(path, user=None, group=None)
Expand Down
1 change: 1 addition & 0 deletions Lib/test/test_shutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -1363,6 +1363,7 @@ def _boo(filename, extract_dir, extra):
"disk_usage not available on this platform")
def test_disk_usage(self):
usage = shutil.disk_usage(os.path.dirname(__file__))
self.assertEqual(usage, shutil.disk_usage(__file__))
self.assertGreater(usage.total, 0)
self.assertGreater(usage.used, 0)
self.assertGreaterEqual(usage.free, 0)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Allow shutil.disk_usage to take a file path on Windows
29 changes: 27 additions & 2 deletions Modules/posixmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -10069,13 +10069,38 @@ os__getdiskusage_impl(PyObject *module, path_t *path)
{
BOOL retval;
ULARGE_INTEGER _, total, free;
DWORD err = 0;

Py_BEGIN_ALLOW_THREADS
retval = GetDiskFreeSpaceExW(path->wide, &_, &total, &free);
Py_END_ALLOW_THREADS
if (retval == 0)
return PyErr_SetFromWindowsErr(0);
if (retval == 0) {
if (GetLastError() == ERROR_DIRECTORY) {
wchar_t *dir_path = NULL;

dir_path = PyMem_New(wchar_t, path->length + 1);
if (dir_path == NULL) {
return PyErr_NoMemory();
}

wcscpy_s(dir_path, path->length + 1, path->wide);

if (_dirnameW(dir_path) != -1) {
Py_BEGIN_ALLOW_THREADS
retval = GetDiskFreeSpaceExW(dir_path, &_, &total, &free);
Py_END_ALLOW_THREADS
}
/* Record the last error in case it's modified by PyMem_Free. */
err = GetLastError();
PyMem_Free(dir_path);
if (retval) {
goto success;
}
}
return PyErr_SetFromWindowsErr(err);
}

success:
return Py_BuildValue("(LL)", total.QuadPart, free.QuadPart);
}
#endif /* MS_WINDOWS */
Expand Down