Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
210e7ca0
Commit
210e7ca0
authored
Jul 01, 2011
by
Giampaolo Rodola
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Issue #12442: add shutil.disk_usage()
parent
59929d98
Changes
6
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
77 additions
and
1 deletion
+77
-1
Doc/library/shutil.rst
Doc/library/shutil.rst
+8
-0
Doc/whatsnew/3.3.rst
Doc/whatsnew/3.3.rst
+10
-1
Lib/shutil.py
Lib/shutil.py
+19
-0
Lib/test/test_shutil.py
Lib/test/test_shutil.py
+10
-0
Misc/NEWS
Misc/NEWS
+3
-0
Modules/posixmodule.c
Modules/posixmodule.c
+27
-0
No files found.
Doc/library/shutil.rst
View file @
210e7ca0
...
...
@@ -164,6 +164,14 @@ Directory and files operations
If the destination is on the current filesystem, then simply use rename.
Otherwise, copy src (with :func:`copy2`) to the dst and then remove src.
.. function:: disk_usage(path)
Return disk usage statistics about the given path as a namedtuple including
total, used and free space expressed in bytes.
.. versionadded:: 3.3
Availability: Unix, Windows.
.. exception:: Error
...
...
Doc/whatsnew/3.3.rst
View file @
210e7ca0
...
...
@@ -200,7 +200,16 @@ The :class:`~ftplib.FTP_TLS` class now provides a new
plaintex. This can be useful to take advantage of firewalls that know how to
handle NAT with non-secure FTP without opening fixed ports.
(Patch submitted by Giampaolo Rodolà in :issue:`12139`.)
(Contributed by Giampaolo Rodolà in :issue:`12139`)
shutil
------
The :mod:`shutil` module has a new :func:`~shutil.disk_usage` providing total,
used and free disk space statistics.
(Contributed by Giampaolo Rodolà in :issue:`12442`)
Optimizations
...
...
Lib/shutil.py
View file @
210e7ca0
...
...
@@ -12,6 +12,7 @@ import fnmatch
import
collections
import
errno
import
tarfile
from
collections
import
namedtuple
try
:
import
bz2
...
...
@@ -754,3 +755,21 @@ def unpack_archive(filename, extract_dir=None, format=None):
func
=
_UNPACK_FORMATS
[
format
][
1
]
kwargs
=
dict
(
_UNPACK_FORMATS
[
format
][
2
])
func
(
filename
,
extract_dir
,
**
kwargs
)
if
hasattr
(
os
,
"statvfs"
)
or
os
.
name
==
'nt'
:
_ntuple_diskusage
=
namedtuple
(
'usage'
,
'total used free'
)
def
disk_usage
(
path
):
"""Return disk usage statistics about the given path as a namedtuple
including total, used and free space expressed in bytes.
"""
if
hasattr
(
os
,
"statvfs"
):
st
=
os
.
statvfs
(
path
)
free
=
(
st
.
f_bavail
*
st
.
f_frsize
)
total
=
(
st
.
f_blocks
*
st
.
f_frsize
)
used
=
(
st
.
f_blocks
-
st
.
f_bfree
)
*
st
.
f_frsize
else
:
import
nt
total
,
free
=
nt
.
_getdiskusage
(
path
)
used
=
total
-
free
return
_ntuple_diskusage
(
total
,
used
,
free
)
Lib/test/test_shutil.py
View file @
210e7ca0
...
...
@@ -728,6 +728,16 @@ class TestShutil(unittest.TestCase):
unregister_unpack_format
(
'Boo2'
)
self
.
assertEqual
(
get_unpack_formats
(),
formats
)
@
unittest
.
skipUnless
(
hasattr
(
shutil
,
'disk_usage'
),
"disk_usage not available on this platform"
)
def
test_disk_usage
(
self
):
usage
=
shutil
.
disk_usage
(
os
.
getcwd
())
self
.
assertTrue
(
usage
.
total
>
0
)
self
.
assertTrue
(
usage
.
used
>
0
)
self
.
assertTrue
(
usage
.
free
>=
0
)
self
.
assertTrue
(
usage
.
total
>=
usage
.
used
)
self
.
assertTrue
(
usage
.
total
>
usage
.
free
)
class
TestMove
(
unittest
.
TestCase
):
...
...
Misc/NEWS
View file @
210e7ca0
...
...
@@ -200,6 +200,9 @@ Core and Builtins
Library
-------
- Issue #12442: new shutil.disk_usage function, providing total, used and free
disk space statistics.
- Issue #12451: The XInclude default loader of xml.etree now decodes files from
UTF-8 instead of the locale encoding if the encoding is not specified. It now
also opens XML files for the parser in binary mode instead of the text mode
...
...
Modules/posixmodule.c
View file @
210e7ca0
...
...
@@ -7451,6 +7451,32 @@ posix_statvfs(PyObject *self, PyObject *args)
}
#endif
/* HAVE_STATVFS */
#ifdef MS_WINDOWS
PyDoc_STRVAR
(
win32__getdiskusage__doc__
,
"_getdiskusage(path) -> (total, free)
\n\n
\
Return disk usage statistics about the given path as (total, free) tuple."
);
static
PyObject
*
win32__getdiskusage
(
PyObject
*
self
,
PyObject
*
args
)
{
BOOL
retval
;
ULARGE_INTEGER
_
,
total
,
free
;
LPCTSTR
path
;
if
(
!
PyArg_ParseTuple
(
args
,
"s"
,
&
path
))
return
NULL
;
Py_BEGIN_ALLOW_THREADS
retval
=
GetDiskFreeSpaceEx
(
path
,
&
_
,
&
total
,
&
free
);
Py_END_ALLOW_THREADS
if
(
retval
==
0
)
return
PyErr_SetFromWindowsErr
(
0
);
return
Py_BuildValue
(
"(LL)"
,
total
.
QuadPart
,
free
.
QuadPart
);
}
#endif
/* This is used for fpathconf(), pathconf(), confstr() and sysconf().
* It maps strings representing configuration variable names to
* integer values, allowing those functions to be called with the
...
...
@@ -9716,6 +9742,7 @@ static PyMethodDef posix_methods[] = {
{
"_getfinalpathname"
,
posix__getfinalpathname
,
METH_VARARGS
,
NULL
},
{
"_getfileinformation"
,
posix__getfileinformation
,
METH_VARARGS
,
NULL
},
{
"_isdir"
,
posix__isdir
,
METH_VARARGS
,
posix__isdir__doc__
},
{
"_getdiskusage"
,
win32__getdiskusage
,
METH_VARARGS
,
win32__getdiskusage__doc__
},
#endif
#ifdef HAVE_GETLOADAVG
{
"getloadavg"
,
posix_getloadavg
,
METH_NOARGS
,
posix_getloadavg__doc__
},
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment