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.rst112
1 files changed, 111 insertions, 1 deletions
diff --git a/Doc/library/pathlib.rst b/Doc/library/pathlib.rst
index 24e2a308df..2f0654440d 100644
--- a/Doc/library/pathlib.rst
+++ b/Doc/library/pathlib.rst
@@ -628,6 +628,17 @@ call fails (for example because the path doesn't exist):
PosixPath('/home/antoine/pathlib')
+.. classmethod:: Path.home()
+
+ Return a new path object representing the user's home directory (as
+ returned by :func:`os.path.expanduser` with ``~`` construct)::
+
+ >>> Path.home()
+ PosixPath('/home/antoine')
+
+ .. versionadded:: 3.5
+
+
.. method:: Path.stat()
Return information about this path (similarly to :func:`os.stat`).
@@ -670,6 +681,18 @@ call fails (for example because the path doesn't exist):
symlink *points to* an existing file or directory.
+.. method:: Path.expanduser()
+
+ Return a new path with expanded ``~`` and ``~user`` constructs,
+ as returned by :meth:`os.path.expanduser`::
+
+ >>> p = PosixPath('~/films/Monty Python')
+ >>> p.expanduser()
+ PosixPath('/home/eric/films/Monty Python')
+
+ .. versionadded:: 3.5
+
+
.. method:: Path.glob(pattern)
Glob the given *pattern* in the directory represented by this path,
@@ -791,7 +814,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 +828,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 +857,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 +945,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 +997,33 @@ 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'
+
+ .. versionadded:: 3.5