Skip to content

Commit d5a7292

Browse files
Use OSError subclasses in os documentation (GH-14262)
(cherry picked from commit a55f75a) Co-authored-by: Tim Hoffmann <[email protected]>
1 parent 2d438fc commit d5a7292

File tree

1 file changed

+24
-17
lines changed

1 file changed

+24
-17
lines changed

Doc/library/os.rst

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@ Notes on the availability of these functions:
3535

3636
.. note::
3737

38-
All functions in this module raise :exc:`OSError` in the case of invalid or
39-
inaccessible file names and paths, or other arguments that have the correct
40-
type, but are not accepted by the operating system.
38+
All functions in this module raise :exc:`OSError` (or subclasses thereof) in
39+
the case of invalid or inaccessible file names and paths, or other arguments
40+
that have the correct type, but are not accepted by the operating system.
4141

4242
.. exception:: error
4343

@@ -1858,8 +1858,8 @@ features:
18581858
directories you can set the umask before invoking :func:`makedirs`. The
18591859
file permission bits of existing parent directories are not changed.
18601860

1861-
If *exist_ok* is ``False`` (the default), an :exc:`OSError` is raised if the
1862-
target directory already exists.
1861+
If *exist_ok* is ``False`` (the default), an :exc:`FileExistsError` is
1862+
raised if the target directory already exists.
18631863

18641864
.. note::
18651865

@@ -2010,8 +2010,8 @@ features:
20102010

20112011
.. function:: remove(path, *, dir_fd=None)
20122012

2013-
Remove (delete) the file *path*. If *path* is a directory, :exc:`OSError` is
2014-
raised. Use :func:`rmdir` to remove directories.
2013+
Remove (delete) the file *path*. If *path* is a directory, an
2014+
:exc:`IsADirectoryError` is raised. Use :func:`rmdir` to remove directories.
20152015

20162016
This function can support :ref:`paths relative to directory descriptors
20172017
<dir_fd>`.
@@ -2048,13 +2048,19 @@ features:
20482048

20492049
.. function:: rename(src, dst, *, src_dir_fd=None, dst_dir_fd=None)
20502050

2051-
Rename the file or directory *src* to *dst*. If *dst* is a directory,
2052-
:exc:`OSError` will be raised. On Unix, if *dst* exists and is a file, it will
2053-
be replaced silently if the user has permission. The operation may fail on some
2054-
Unix flavors if *src* and *dst* are on different filesystems. If successful,
2055-
the renaming will be an atomic operation (this is a POSIX requirement). On
2056-
Windows, if *dst* already exists, :exc:`OSError` will be raised even if it is a
2057-
file.
2051+
Rename the file or directory *src* to *dst*. If *dst* exists, the operation
2052+
will fail with an :exc:`OSError` subclass in a number of cases:
2053+
2054+
On Windows, if *dst* exists a :exc:`FileExistsError` is always raised.
2055+
2056+
On Unix, if *src* is a file and *dst* is a directory or vice-versa, anq:q
2057+
:exc:`IsADirectoryError` or a :exc:`NotADirectoryError` will be raised
2058+
respectively. If both are directories and *dst* is empty, *dst* will be
2059+
silently replaced. If *dst* is a non-empty directory, an :exc:`OSError`
2060+
is raised. If both are files, *dst* it will be replaced silently if the user
2061+
has permission. The operation may fail on some Unix flavors if *src* and
2062+
*dst* are on different filesystems. If successful, the renaming will be an
2063+
atomic operation (this is a POSIX requirement).
20582064

20592065
This function can support specifying *src_dir_fd* and/or *dst_dir_fd* to
20602066
supply :ref:`paths relative to directory descriptors <dir_fd>`.
@@ -2103,9 +2109,10 @@ features:
21032109

21042110
.. function:: rmdir(path, *, dir_fd=None)
21052111

2106-
Remove (delete) the directory *path*. Only works when the directory is
2107-
empty, otherwise, :exc:`OSError` is raised. In order to remove whole
2108-
directory trees, :func:`shutil.rmtree` can be used.
2112+
Remove (delete) the directory *path*. If the directory does not exist or is
2113+
not empty, an :exc:`FileNotFoundError` or an :exc:`OSError` is raised
2114+
respectively. In order to remove whole directory trees,
2115+
:func:`shutil.rmtree` can be used.
21092116

21102117
This function can support :ref:`paths relative to directory descriptors
21112118
<dir_fd>`.

0 commit comments

Comments
 (0)