summaryrefslogtreecommitdiff
path: root/Doc/library/pathlib.rst
diff options
context:
space:
mode:
Diffstat (limited to 'Doc/library/pathlib.rst')
-rw-r--r--Doc/library/pathlib.rst92
1 files changed, 91 insertions, 1 deletions
diff --git a/Doc/library/pathlib.rst b/Doc/library/pathlib.rst
index ec1dc4f616..0942b21df3 100644
--- a/Doc/library/pathlib.rst
+++ b/Doc/library/pathlib.rst
@@ -791,7 +791,7 @@ call fails (for example because the path doesn't exist):
the symbolic link's information rather than its target's.
-.. method:: Path.mkdir(mode=0o777, parents=False)
+.. method:: Path.mkdir(mode=0o777, parents=False, exist_ok=False)
Create a new directory at this given path. If *mode* is given, it is
combined with the process' ``umask`` value to determine the file mode
@@ -805,6 +805,16 @@ call fails (for example because the path doesn't exist):
If *parents* is false (the default), a missing parent raises
:exc:`FileNotFoundError`.
+ If *exist_ok* is false (the default), an :exc:`FileExistsError` is
+ raised if the target directory already exists.
+
+ If *exist_ok* is true, :exc:`FileExistsError` exceptions will be
+ ignored (same behavior as the POSIX ``mkdir -p`` command), but only if the
+ last path component is not an existing non-directory file.
+
+ .. versionchanged:: 3.5
+ The *exist_ok* parameter was added.
+
.. method:: Path.open(mode='r', buffering=-1, encoding=None, errors=None, newline=None)
@@ -824,6 +834,34 @@ call fails (for example because the path doesn't exist):
if the file's uid isn't found in the system database.
+.. method:: Path.read_bytes()
+
+ Return the binary contents of the pointed-to file as a bytes object::
+
+ >>> p = Path('my_binary_file')
+ >>> p.write_bytes(b'Binary file contents')
+ 20
+ >>> p.read_bytes()
+ b'Binary file contents'
+
+ .. versionadded:: 3.5
+
+
+.. method:: Path.read_text(encoding=None, errors=None)
+
+ Return the decoded contents of the pointed-to file as a string::
+
+ >>> p = Path('my_text_file')
+ >>> p.write_text('Text file contents')
+ 18
+ >>> p.read_text()
+ 'Text file contents'
+
+ The optional parameters have the same meaning as in :func:`open`.
+
+ .. versionadded:: 3.5
+
+
.. method:: Path.rename(target)
Rename this file or directory to the given *target*. *target* can be
@@ -884,6 +922,25 @@ call fails (for example because the path doesn't exist):
Remove this directory. The directory must be empty.
+.. method:: Path.samefile(other_path)
+
+ Return whether this path points to the same file as *other_path*, which
+ can be either a Path object, or a string. The semantics are similar
+ to :func:`os.path.samefile` and :func:`os.path.samestat`.
+
+ An :exc:`OSError` can be raised if either file cannot be accessed for some
+ reason.
+
+ >>> p = Path('spam')
+ >>> q = Path('eggs')
+ >>> p.samefile(q)
+ False
+ >>> p.samefile('spam')
+ True
+
+ .. versionadded:: 3.5
+
+
.. method:: Path.symlink_to(target, target_is_directory=False)
Make this path a symbolic link to *target*. Under Windows,
@@ -917,3 +974,36 @@ call fails (for example because the path doesn't exist):
Remove this file or symbolic link. If the path points to a directory,
use :func:`Path.rmdir` instead.
+
+
+.. method:: Path.write_bytes(data)
+
+ Open the file pointed to in bytes mode, write *data* to it, and close the
+ file::
+
+ >>> p = Path('my_binary_file')
+ >>> p.write_bytes(b'Binary file contents')
+ 20
+ >>> p.read_bytes()
+ b'Binary file contents'
+
+ An existing file of the same name is overwritten.
+
+ .. versionadded:: 3.5
+
+
+.. method:: Path.write_text(data, encoding=None, errors=None)
+
+ Open the file pointed to in text mode, write *data* to it, and close the
+ file::
+
+ >>> p = Path('my_text_file')
+ >>> p.write_text('Text file contents')
+ 18
+ >>> p.read_text()
+ 'Text file contents'
+
+ An existing file of the same name is overwritten. The optional parameters
+ have the same meaning as in :func:`open`.
+
+ .. versionadded:: 3.5