2012-10-05 14:19:40 -04:00
|
|
|
PEP: 428
|
|
|
|
Title: The pathlib module -- object-oriented filesystem paths
|
|
|
|
Version: $Revision$
|
2012-10-05 17:29:21 -04:00
|
|
|
Last-Modified: $Date$
|
2012-10-05 14:19:40 -04:00
|
|
|
Author: Antoine Pitrou <solipsis@pitrou.net>
|
2013-11-22 12:30:31 -05:00
|
|
|
Status: Final
|
2012-10-05 14:19:40 -04:00
|
|
|
Type: Standards Track
|
|
|
|
Content-Type: text/x-rst
|
|
|
|
Created: 30-July-2012
|
|
|
|
Python-Version: 3.4
|
2012-10-05 19:04:50 -04:00
|
|
|
Post-History: http://mail.python.org/pipermail/python-ideas/2012-October/016338.html
|
2013-11-20 16:44:22 -05:00
|
|
|
Resolution: https://mail.python.org/pipermail/python-dev/2013-November/130424.html
|
2012-10-05 14:19:40 -04:00
|
|
|
|
|
|
|
|
|
|
|
Abstract
|
|
|
|
========
|
|
|
|
|
|
|
|
This PEP proposes the inclusion of a third-party module, `pathlib`_, in
|
|
|
|
the standard library. The inclusion is proposed under the provisional
|
|
|
|
label, as described in :pep:`411`. Therefore, API changes can be done,
|
|
|
|
either as part of the PEP process, or after acceptance in the standard
|
|
|
|
library (and until the provisional label is removed).
|
|
|
|
|
|
|
|
The aim of this library is to provide a simple hierarchy of classes to
|
|
|
|
handle filesystem paths and the common operations users do over them.
|
|
|
|
|
|
|
|
.. _`pathlib`: http://pypi.python.org/pypi/pathlib/
|
|
|
|
|
|
|
|
|
|
|
|
Related work
|
|
|
|
============
|
|
|
|
|
|
|
|
An object-oriented API for filesystem paths has already been proposed
|
|
|
|
and rejected in :pep:`355`. Several third-party implementations of the
|
|
|
|
idea of object-oriented filesystem paths exist in the wild:
|
|
|
|
|
|
|
|
* The historical `path.py module`_ by Jason Orendorff, Jason R. Coombs
|
|
|
|
and others, which provides a ``str``-subclassing ``Path`` class;
|
|
|
|
|
|
|
|
* Twisted's slightly specialized `FilePath class`_;
|
|
|
|
|
|
|
|
* An `AlternativePathClass proposal`_, subclassing ``tuple`` rather than
|
|
|
|
``str``;
|
|
|
|
|
|
|
|
* `Unipath`_, a variation on the str-subclassing approach with two public
|
|
|
|
classes, an ``AbstractPath`` class for operations which don't do I/O and a
|
|
|
|
``Path`` class for all common operations.
|
|
|
|
|
|
|
|
This proposal attempts to learn from these previous attempts and the
|
|
|
|
rejection of :pep:`355`.
|
|
|
|
|
|
|
|
|
|
|
|
.. _`path.py module`: https://github.com/jaraco/path.py
|
|
|
|
.. _`FilePath class`: http://twistedmatrix.com/documents/current/api/twisted.python.filepath.FilePath.html
|
|
|
|
.. _`AlternativePathClass proposal`: http://wiki.python.org/moin/AlternativePathClass
|
|
|
|
.. _`Unipath`: https://bitbucket.org/sluggo/unipath/overview
|
|
|
|
|
|
|
|
|
2013-03-01 17:24:24 -05:00
|
|
|
Implementation
|
|
|
|
==============
|
|
|
|
|
|
|
|
The implementation of this proposal is tracked in the ``pep428`` branch
|
|
|
|
of pathlib's `Mercurial repository`_.
|
|
|
|
|
|
|
|
.. _`Mercurial repository`: https://bitbucket.org/pitrou/pathlib/
|
|
|
|
|
|
|
|
|
2012-10-05 14:19:40 -04:00
|
|
|
Why an object-oriented API
|
|
|
|
==========================
|
|
|
|
|
|
|
|
The rationale to represent filesystem paths using dedicated classes is the
|
|
|
|
same as for other kinds of stateless objects, such as dates, times or IP
|
|
|
|
addresses. Python has been slowly moving away from strictly replicating
|
|
|
|
the C language's APIs to providing better, more helpful abstractions around
|
|
|
|
all kinds of common functionality. Even if this PEP isn't accepted, it is
|
|
|
|
likely that another form of filesystem handling abstraction will be adopted
|
|
|
|
one day into the standard library.
|
|
|
|
|
|
|
|
Indeed, many people will prefer handling dates and times using the high-level
|
|
|
|
objects provided by the ``datetime`` module, rather than using numeric
|
|
|
|
timestamps and the ``time`` module API. Moreover, using a dedicated class
|
|
|
|
allows to enable desirable behaviours by default, for example the case
|
|
|
|
insensitivity of Windows paths.
|
|
|
|
|
|
|
|
|
|
|
|
Proposal
|
|
|
|
========
|
|
|
|
|
|
|
|
Class hierarchy
|
|
|
|
---------------
|
|
|
|
|
|
|
|
The `pathlib`_ module implements a simple hierarchy of classes::
|
|
|
|
|
|
|
|
+----------+
|
|
|
|
| |
|
|
|
|
---------| PurePath |--------
|
|
|
|
| | | |
|
|
|
|
| +----------+ |
|
|
|
|
| | |
|
|
|
|
| | |
|
|
|
|
v | v
|
2013-11-16 16:31:45 -05:00
|
|
|
+---------------+ | +-----------------+
|
|
|
|
| | | | |
|
|
|
|
| PurePosixPath | | | PureWindowsPath |
|
|
|
|
| | | | |
|
|
|
|
+---------------+ | +-----------------+
|
2012-10-05 14:19:40 -04:00
|
|
|
| v |
|
|
|
|
| +------+ |
|
|
|
|
| | | |
|
|
|
|
| -------| Path |------ |
|
|
|
|
| | | | | |
|
|
|
|
| | +------+ | |
|
|
|
|
| | | |
|
|
|
|
| | | |
|
|
|
|
v v v v
|
2013-11-16 16:31:45 -05:00
|
|
|
+-----------+ +-------------+
|
|
|
|
| | | |
|
|
|
|
| PosixPath | | WindowsPath |
|
|
|
|
| | | |
|
|
|
|
+-----------+ +-------------+
|
2012-10-05 14:19:40 -04:00
|
|
|
|
|
|
|
|
|
|
|
This hierarchy divides path classes along two dimensions:
|
|
|
|
|
|
|
|
* a path class can be either pure or concrete: pure classes support only
|
|
|
|
operations that don't need to do any actual I/O, which are most path
|
|
|
|
manipulation operations; concrete classes support all the operations
|
|
|
|
of pure classes, plus operations that do I/O.
|
|
|
|
|
|
|
|
* a path class is of a given flavour according to the kind of operating
|
2013-11-19 10:40:46 -05:00
|
|
|
system paths it represents. `pathlib`_ implements two flavours: Windows
|
|
|
|
paths for the filesystem semantics embodied in Windows systems, POSIX
|
|
|
|
paths for other systems.
|
2012-10-05 14:19:40 -04:00
|
|
|
|
|
|
|
Any pure class can be instantiated on any system: for example, you can
|
2013-11-16 16:31:45 -05:00
|
|
|
manipulate ``PurePosixPath`` objects under Windows, ``PureWindowsPath``
|
|
|
|
objects under Unix, and so on. However, concrete classes can only be
|
|
|
|
instantiated on a matching system: indeed, it would be error-prone to start
|
|
|
|
doing I/O with ``WindowsPath`` objects under Unix, or vice-versa.
|
2012-10-05 14:19:40 -04:00
|
|
|
|
|
|
|
Furthermore, there are two base classes which also act as system-dependent
|
|
|
|
factories: ``PurePath`` will instantiate either a ``PurePosixPath`` or a
|
2013-11-16 16:31:45 -05:00
|
|
|
``PureWindowsPath`` depending on the operating system. Similarly, ``Path``
|
|
|
|
will instantiate either a ``PosixPath`` or a ``WindowsPath``.
|
2012-10-05 14:19:40 -04:00
|
|
|
|
|
|
|
It is expected that, in most uses, using the ``Path`` class is adequate,
|
|
|
|
which is why it has the shortest name of all.
|
|
|
|
|
|
|
|
|
|
|
|
No confusion with builtins
|
|
|
|
--------------------------
|
|
|
|
|
|
|
|
In this proposal, the path classes do not derive from a builtin type. This
|
|
|
|
contrasts with some other Path class proposals which were derived from
|
|
|
|
``str``. They also do not pretend to implement the sequence protocol:
|
2013-11-19 17:05:51 -05:00
|
|
|
if you want a path to act as a sequence, you have to lookup a dedicated
|
2012-10-05 14:19:40 -04:00
|
|
|
attribute (the ``parts`` attribute).
|
|
|
|
|
2016-04-23 12:04:27 -04:00
|
|
|
The key reasoning behind not inheriting from ``str`` is to prevent accidentally
|
|
|
|
performing operations with a string representing a path and a string that
|
|
|
|
doesn't, e.g. ``path + an_accident``. Since operations with a string will not
|
|
|
|
necessarily lead to a valid or expected file system path, "explicit is better
|
|
|
|
than implicit" by avoiding accidental operations with strings by not
|
|
|
|
subclassing it. A `blog post`_ by a Python core developer goes into more detail
|
|
|
|
on the reasons behind this specific design decision.
|
2016-04-22 14:45:53 -04:00
|
|
|
|
|
|
|
.. _blog post: http://www.snarky.ca/why-pathlib-path-doesn-t-inherit-from-str
|
2012-10-05 14:19:40 -04:00
|
|
|
|
|
|
|
|
|
|
|
Immutability
|
|
|
|
------------
|
|
|
|
|
|
|
|
Path objects are immutable, which makes them hashable and also prevents a
|
|
|
|
class of programming errors.
|
|
|
|
|
|
|
|
|
|
|
|
Sane behaviour
|
|
|
|
--------------
|
|
|
|
|
|
|
|
Little of the functionality from os.path is reused. Many os.path functions
|
|
|
|
are tied by backwards compatibility to confusing or plain wrong behaviour
|
|
|
|
(for example, the fact that ``os.path.abspath()`` simplifies ".." path
|
|
|
|
components without resolving symlinks first).
|
|
|
|
|
2012-10-05 19:13:30 -04:00
|
|
|
|
|
|
|
Comparisons
|
|
|
|
-----------
|
|
|
|
|
|
|
|
Paths of the same flavour are comparable and orderable, whether pure or not::
|
|
|
|
|
|
|
|
>>> PurePosixPath('a') == PurePosixPath('b')
|
|
|
|
False
|
|
|
|
>>> PurePosixPath('a') < PurePosixPath('b')
|
|
|
|
True
|
|
|
|
>>> PurePosixPath('a') == PosixPath('a')
|
|
|
|
True
|
|
|
|
|
|
|
|
Comparing and ordering Windows path objects is case-insensitive::
|
|
|
|
|
2013-11-16 16:31:45 -05:00
|
|
|
>>> PureWindowsPath('a') == PureWindowsPath('A')
|
2012-10-05 19:13:30 -04:00
|
|
|
True
|
|
|
|
|
|
|
|
Paths of different flavours always compare unequal, and cannot be ordered::
|
|
|
|
|
2013-11-16 16:31:45 -05:00
|
|
|
>>> PurePosixPath('a') == PureWindowsPath('a')
|
2012-10-05 19:13:30 -04:00
|
|
|
False
|
2013-11-16 16:31:45 -05:00
|
|
|
>>> PurePosixPath('a') < PureWindowsPath('a')
|
2012-10-05 19:13:30 -04:00
|
|
|
Traceback (most recent call last):
|
|
|
|
File "<stdin>", line 1, in <module>
|
2013-11-16 16:31:45 -05:00
|
|
|
TypeError: unorderable types: PurePosixPath() < PureWindowsPath()
|
2012-10-05 14:19:40 -04:00
|
|
|
|
2013-11-19 18:46:39 -05:00
|
|
|
Paths compare unequal to, and are not orderable with instances of builtin
|
|
|
|
types (such as ``str``) and any other types.
|
|
|
|
|
2012-10-05 14:19:40 -04:00
|
|
|
|
|
|
|
Useful notations
|
|
|
|
----------------
|
|
|
|
|
|
|
|
The API tries to provide useful notations all the while avoiding magic.
|
|
|
|
Some examples::
|
|
|
|
|
|
|
|
>>> p = Path('/home/antoine/pathlib/setup.py')
|
|
|
|
>>> p.name
|
|
|
|
'setup.py'
|
2013-11-19 17:04:11 -05:00
|
|
|
>>> p.suffix
|
2012-10-05 14:19:40 -04:00
|
|
|
'.py'
|
|
|
|
>>> p.root
|
|
|
|
'/'
|
|
|
|
>>> p.parts
|
2013-11-19 15:48:55 -05:00
|
|
|
('/', 'home', 'antoine', 'pathlib', 'setup.py')
|
2013-11-16 13:32:39 -05:00
|
|
|
>>> p.relative_to('/home/antoine')
|
2013-10-25 15:03:28 -04:00
|
|
|
PosixPath('pathlib/setup.py')
|
2012-10-05 14:19:40 -04:00
|
|
|
>>> p.exists()
|
|
|
|
True
|
|
|
|
|
|
|
|
|
|
|
|
Pure paths API
|
|
|
|
==============
|
|
|
|
|
|
|
|
The philosophy of the ``PurePath`` API is to provide a consistent array of
|
|
|
|
useful path manipulation operations, without exposing a hodge-podge of
|
|
|
|
functions like ``os.path`` does.
|
|
|
|
|
|
|
|
|
|
|
|
Definitions
|
|
|
|
-----------
|
|
|
|
|
|
|
|
First a couple of conventions:
|
|
|
|
|
|
|
|
* All paths can have a drive and a root. For POSIX paths, the drive is
|
|
|
|
always empty.
|
|
|
|
|
|
|
|
* A relative path has neither drive nor root.
|
|
|
|
|
|
|
|
* A POSIX path is absolute if it has a root. A Windows path is absolute if
|
|
|
|
it has both a drive *and* a root. A Windows UNC path (e.g.
|
2013-11-17 10:33:06 -05:00
|
|
|
``\\host\share\myfile.txt``) always has a drive and a root
|
|
|
|
(here, ``\\host\share`` and ``\``, respectively).
|
2012-10-05 14:19:40 -04:00
|
|
|
|
2013-10-25 15:03:50 -04:00
|
|
|
* A path which has either a drive *or* a root is said to be anchored.
|
2012-10-05 14:19:40 -04:00
|
|
|
Its anchor is the concatenation of the drive and root. Under POSIX,
|
|
|
|
"anchored" is the same as "absolute".
|
|
|
|
|
|
|
|
|
2012-10-05 19:13:30 -04:00
|
|
|
Construction
|
|
|
|
------------
|
2012-10-05 14:19:40 -04:00
|
|
|
|
|
|
|
We will present construction and joining together since they expose
|
|
|
|
similar semantics.
|
|
|
|
|
|
|
|
The simplest way to construct a path is to pass it its string representation::
|
|
|
|
|
|
|
|
>>> PurePath('setup.py')
|
|
|
|
PurePosixPath('setup.py')
|
|
|
|
|
|
|
|
Extraneous path separators and ``"."`` components are eliminated::
|
|
|
|
|
|
|
|
>>> PurePath('a///b/c/./d/')
|
|
|
|
PurePosixPath('a/b/c/d')
|
|
|
|
|
|
|
|
If you pass several arguments, they will be automatically joined::
|
|
|
|
|
|
|
|
>>> PurePath('docs', 'Makefile')
|
|
|
|
PurePosixPath('docs/Makefile')
|
|
|
|
|
|
|
|
Joining semantics are similar to os.path.join, in that anchored paths ignore
|
|
|
|
the information from the previously joined components::
|
|
|
|
|
|
|
|
>>> PurePath('/etc', '/usr', 'bin')
|
|
|
|
PurePosixPath('/usr/bin')
|
|
|
|
|
|
|
|
However, with Windows paths, the drive is retained as necessary::
|
|
|
|
|
2013-11-16 16:31:45 -05:00
|
|
|
>>> PureWindowsPath('c:/foo', '/Windows')
|
2013-11-17 10:33:06 -05:00
|
|
|
PureWindowsPath('c:/Windows')
|
2013-11-16 16:31:45 -05:00
|
|
|
>>> PureWindowsPath('c:/foo', 'd:')
|
|
|
|
PureWindowsPath('d:')
|
2012-10-05 14:19:40 -04:00
|
|
|
|
2012-10-05 19:13:30 -04:00
|
|
|
Also, path separators are normalized to the platform default::
|
|
|
|
|
2013-11-16 16:31:45 -05:00
|
|
|
>>> PureWindowsPath('a/b') == PureWindowsPath('a\\b')
|
2012-10-05 19:13:30 -04:00
|
|
|
True
|
|
|
|
|
|
|
|
Extraneous path separators and ``"."`` components are eliminated, but not
|
|
|
|
``".."`` components::
|
|
|
|
|
|
|
|
>>> PurePosixPath('a//b/./c/')
|
|
|
|
PurePosixPath('a/b/c')
|
|
|
|
>>> PurePosixPath('a/../b')
|
|
|
|
PurePosixPath('a/../b')
|
|
|
|
|
2012-10-05 19:17:59 -04:00
|
|
|
Multiple leading slashes are treated differently depending on the path
|
2012-12-28 19:46:48 -05:00
|
|
|
flavour. They are always retained on Windows paths (because of the UNC
|
|
|
|
notation)::
|
2012-10-05 19:17:59 -04:00
|
|
|
|
2013-11-16 16:31:45 -05:00
|
|
|
>>> PureWindowsPath('//some/path')
|
2013-11-17 10:33:06 -05:00
|
|
|
PureWindowsPath('//some/path/')
|
2012-10-05 19:17:59 -04:00
|
|
|
|
2012-12-28 19:46:48 -05:00
|
|
|
On POSIX, they are collapsed except if there are exactly two leading slashes,
|
|
|
|
which is a special case in the POSIX specification on `pathname resolution`_
|
|
|
|
(this is also necessary for Cygwin compatibility)::
|
|
|
|
|
|
|
|
>>> PurePosixPath('///some/path')
|
|
|
|
PurePosixPath('/some/path')
|
|
|
|
>>> PurePosixPath('//some/path')
|
|
|
|
PurePosixPath('//some/path')
|
|
|
|
|
2012-10-05 14:19:40 -04:00
|
|
|
Calling the constructor without any argument creates a path object pointing
|
2013-11-19 18:46:39 -05:00
|
|
|
to the logical "current directory" (without looking up its absolute path,
|
|
|
|
which is the job of the ``cwd()`` classmethod on concrete paths)::
|
2012-10-05 14:19:40 -04:00
|
|
|
|
|
|
|
>>> PurePosixPath()
|
|
|
|
PurePosixPath('.')
|
|
|
|
|
2012-12-28 19:46:48 -05:00
|
|
|
.. _pathname resolution: http://pubs.opengroup.org/onlinepubs/009695399/basedefs/xbd_chap04.html#tag_04_11
|
|
|
|
|
2012-10-05 19:13:30 -04:00
|
|
|
|
2012-10-05 14:19:40 -04:00
|
|
|
Representing
|
|
|
|
------------
|
|
|
|
|
|
|
|
To represent a path (e.g. to pass it to third-party libraries), just call
|
|
|
|
``str()`` on it::
|
|
|
|
|
|
|
|
>>> p = PurePath('/home/antoine/pathlib/setup.py')
|
|
|
|
>>> str(p)
|
|
|
|
'/home/antoine/pathlib/setup.py'
|
2013-11-16 16:31:45 -05:00
|
|
|
>>> p = PureWindowsPath('c:/windows')
|
2012-10-05 14:19:40 -04:00
|
|
|
>>> str(p)
|
|
|
|
'c:\\windows'
|
|
|
|
|
|
|
|
To force the string representation with forward slashes, use the ``as_posix()``
|
|
|
|
method::
|
|
|
|
|
|
|
|
>>> p.as_posix()
|
|
|
|
'c:/windows'
|
|
|
|
|
|
|
|
To get the bytes representation (which might be useful under Unix systems),
|
2013-11-17 10:33:06 -05:00
|
|
|
call ``bytes()`` on it, which internally uses ``os.fsencode()``::
|
2012-10-05 14:19:40 -04:00
|
|
|
|
|
|
|
>>> bytes(p)
|
|
|
|
b'/home/antoine/pathlib/setup.py'
|
|
|
|
|
2013-11-17 10:33:06 -05:00
|
|
|
To represent the path as a ``file:`` URI, call the ``as_uri()`` method::
|
2013-03-01 17:10:53 -05:00
|
|
|
|
|
|
|
>>> p = PurePosixPath('/etc/passwd')
|
|
|
|
>>> p.as_uri()
|
|
|
|
'file:///etc/passwd'
|
2013-11-16 16:31:45 -05:00
|
|
|
>>> p = PureWindowsPath('c:/Windows')
|
2013-03-01 17:10:53 -05:00
|
|
|
>>> p.as_uri()
|
|
|
|
'file:///c:/Windows'
|
|
|
|
|
2013-11-17 10:33:06 -05:00
|
|
|
The repr() of a path always uses forward slashes, even under Windows, for
|
|
|
|
readability and to remind users that forward slashes are ok::
|
|
|
|
|
|
|
|
>>> p = PureWindowsPath('c:/Windows')
|
|
|
|
>>> p
|
|
|
|
PureWindowsPath('c:/Windows')
|
|
|
|
|
2012-10-05 14:19:40 -04:00
|
|
|
|
|
|
|
Properties
|
|
|
|
----------
|
|
|
|
|
2013-11-19 10:45:47 -05:00
|
|
|
Several simple properties are provided on every path (each can be empty)::
|
2012-10-05 14:19:40 -04:00
|
|
|
|
2013-11-16 16:31:45 -05:00
|
|
|
>>> p = PureWindowsPath('c:/Downloads/pathlib.tar.gz')
|
2012-10-05 14:19:40 -04:00
|
|
|
>>> p.drive
|
|
|
|
'c:'
|
|
|
|
>>> p.root
|
|
|
|
'\\'
|
|
|
|
>>> p.anchor
|
|
|
|
'c:\\'
|
|
|
|
>>> p.name
|
2012-10-10 14:34:57 -04:00
|
|
|
'pathlib.tar.gz'
|
2013-10-27 11:56:12 -04:00
|
|
|
>>> p.stem
|
2012-10-10 14:34:57 -04:00
|
|
|
'pathlib.tar'
|
|
|
|
>>> p.suffix
|
|
|
|
'.gz'
|
|
|
|
>>> p.suffixes
|
|
|
|
['.tar', '.gz']
|
2012-10-05 14:19:40 -04:00
|
|
|
|
|
|
|
|
2012-10-10 14:55:44 -04:00
|
|
|
Deriving new paths
|
|
|
|
------------------
|
|
|
|
|
|
|
|
Joining
|
|
|
|
^^^^^^^
|
|
|
|
|
2012-11-07 14:25:27 -05:00
|
|
|
A path can be joined with another using the ``/`` operator::
|
2012-10-10 14:55:44 -04:00
|
|
|
|
|
|
|
>>> p = PurePosixPath('foo')
|
2012-11-07 14:25:27 -05:00
|
|
|
>>> p / 'bar'
|
2012-10-10 14:55:44 -04:00
|
|
|
PurePosixPath('foo/bar')
|
2012-11-07 14:25:27 -05:00
|
|
|
>>> p / PurePosixPath('bar')
|
2012-10-10 14:55:44 -04:00
|
|
|
PurePosixPath('foo/bar')
|
2012-11-07 14:25:27 -05:00
|
|
|
>>> 'bar' / p
|
|
|
|
PurePosixPath('bar/foo')
|
2012-10-10 14:55:44 -04:00
|
|
|
|
|
|
|
As with the constructor, multiple path components can be specified, either
|
|
|
|
collapsed or separately::
|
|
|
|
|
2012-11-07 14:25:27 -05:00
|
|
|
>>> p / 'bar/xyzzy'
|
2012-10-10 14:55:44 -04:00
|
|
|
PurePosixPath('foo/bar/xyzzy')
|
2012-11-07 14:25:27 -05:00
|
|
|
>>> p / 'bar' / 'xyzzy'
|
2012-10-10 14:55:44 -04:00
|
|
|
PurePosixPath('foo/bar/xyzzy')
|
|
|
|
|
2013-10-25 15:05:22 -04:00
|
|
|
A joinpath() method is also provided, with the same behaviour::
|
2012-10-10 14:55:44 -04:00
|
|
|
|
2013-10-25 15:05:22 -04:00
|
|
|
>>> p.joinpath('Python')
|
|
|
|
PurePosixPath('foo/Python')
|
2012-10-10 14:55:44 -04:00
|
|
|
|
2012-10-10 15:01:50 -04:00
|
|
|
Changing the path's final component
|
|
|
|
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
2012-10-10 14:55:44 -04:00
|
|
|
|
|
|
|
The ``with_name()`` method returns a new path, with the name changed::
|
|
|
|
|
2013-11-16 16:31:45 -05:00
|
|
|
>>> p = PureWindowsPath('c:/Downloads/pathlib.tar.gz')
|
2012-10-10 14:55:44 -04:00
|
|
|
>>> p.with_name('setup.py')
|
2013-11-17 10:33:06 -05:00
|
|
|
PureWindowsPath('c:/Downloads/setup.py')
|
2012-10-10 14:55:44 -04:00
|
|
|
|
|
|
|
It fails with a ``ValueError`` if the path doesn't have an actual name::
|
|
|
|
|
2013-11-16 16:31:45 -05:00
|
|
|
>>> p = PureWindowsPath('c:/')
|
2012-10-10 14:55:44 -04:00
|
|
|
>>> p.with_name('setup.py')
|
|
|
|
Traceback (most recent call last):
|
|
|
|
File "<stdin>", line 1, in <module>
|
|
|
|
File "pathlib.py", line 875, in with_name
|
|
|
|
raise ValueError("%r has an empty name" % (self,))
|
2013-11-17 10:33:06 -05:00
|
|
|
ValueError: PureWindowsPath('c:/') has an empty name
|
2012-10-10 14:55:44 -04:00
|
|
|
>>> p.name
|
|
|
|
''
|
|
|
|
|
|
|
|
The ``with_suffix()`` method returns a new path with the suffix changed.
|
|
|
|
However, if the path has no suffix, the new suffix is added::
|
|
|
|
|
2013-11-16 16:31:45 -05:00
|
|
|
>>> p = PureWindowsPath('c:/Downloads/pathlib.tar.gz')
|
2012-10-10 14:55:44 -04:00
|
|
|
>>> p.with_suffix('.bz2')
|
2013-11-17 10:33:06 -05:00
|
|
|
PureWindowsPath('c:/Downloads/pathlib.tar.bz2')
|
2013-11-16 16:31:45 -05:00
|
|
|
>>> p = PureWindowsPath('README')
|
2012-10-10 14:55:44 -04:00
|
|
|
>>> p.with_suffix('.bz2')
|
2013-11-16 16:31:45 -05:00
|
|
|
PureWindowsPath('README.bz2')
|
2012-10-10 14:55:44 -04:00
|
|
|
|
2012-10-10 15:01:50 -04:00
|
|
|
Making the path relative
|
|
|
|
^^^^^^^^^^^^^^^^^^^^^^^^
|
|
|
|
|
2013-11-04 10:19:23 -05:00
|
|
|
The ``relative_to()`` method computes the relative difference of a path to
|
2012-10-10 15:01:50 -04:00
|
|
|
another::
|
|
|
|
|
2013-11-04 10:19:23 -05:00
|
|
|
>>> PurePosixPath('/usr/bin/python').relative_to('/usr')
|
2012-10-10 15:01:50 -04:00
|
|
|
PurePosixPath('bin/python')
|
|
|
|
|
|
|
|
ValueError is raised if the method cannot return a meaningful value::
|
|
|
|
|
2013-11-04 10:19:23 -05:00
|
|
|
>>> PurePosixPath('/usr/bin/python').relative_to('/etc')
|
2012-10-10 15:01:50 -04:00
|
|
|
Traceback (most recent call last):
|
|
|
|
File "<stdin>", line 1, in <module>
|
2013-11-04 10:19:23 -05:00
|
|
|
File "pathlib.py", line 926, in relative_to
|
2012-10-10 15:01:50 -04:00
|
|
|
.format(str(self), str(formatted)))
|
|
|
|
ValueError: '/usr/bin/python' does not start with '/etc'
|
|
|
|
|
2012-10-10 14:55:44 -04:00
|
|
|
|
2012-10-05 14:19:40 -04:00
|
|
|
Sequence-like access
|
|
|
|
--------------------
|
|
|
|
|
2013-11-19 15:48:55 -05:00
|
|
|
The ``parts`` property returns a tuple providing read-only sequence access
|
|
|
|
to a path's components::
|
2012-10-05 14:19:40 -04:00
|
|
|
|
|
|
|
>>> p = PurePosixPath('/etc/init.d')
|
|
|
|
>>> p.parts
|
2013-11-19 15:48:55 -05:00
|
|
|
('/', 'etc', 'init.d')
|
2012-10-05 14:19:40 -04:00
|
|
|
|
|
|
|
Windows paths handle the drive and the root as a single path component::
|
|
|
|
|
2013-11-16 16:31:45 -05:00
|
|
|
>>> p = PureWindowsPath('c:/setup.py')
|
2012-10-05 14:19:40 -04:00
|
|
|
>>> p.parts
|
2013-11-19 15:48:55 -05:00
|
|
|
('c:\\', 'setup.py')
|
2012-10-05 14:19:40 -04:00
|
|
|
|
|
|
|
(separating them would be wrong, since ``C:`` is not the parent of ``C:\\``).
|
|
|
|
|
2013-11-19 16:01:14 -05:00
|
|
|
The ``parent`` property returns the logical parent of the path::
|
2012-10-05 14:19:40 -04:00
|
|
|
|
2013-11-19 16:01:14 -05:00
|
|
|
>>> p = PureWindowsPath('c:/python33/bin/python.exe')
|
|
|
|
>>> p.parent
|
2013-11-17 10:33:06 -05:00
|
|
|
PureWindowsPath('c:/python33/bin')
|
2012-10-05 14:19:40 -04:00
|
|
|
|
2013-11-19 15:51:19 -05:00
|
|
|
The ``parents`` property returns an immutable sequence of the path's
|
|
|
|
logical ancestors::
|
2012-10-05 14:19:40 -04:00
|
|
|
|
2013-11-16 16:31:45 -05:00
|
|
|
>>> p = PureWindowsPath('c:/python33/bin/python.exe')
|
2013-11-19 15:51:19 -05:00
|
|
|
>>> len(p.parents)
|
|
|
|
3
|
|
|
|
>>> p.parents[0]
|
2013-11-17 10:33:06 -05:00
|
|
|
PureWindowsPath('c:/python33/bin')
|
2013-11-19 15:51:19 -05:00
|
|
|
>>> p.parents[1]
|
2013-11-17 10:33:06 -05:00
|
|
|
PureWindowsPath('c:/python33')
|
2013-11-19 15:51:19 -05:00
|
|
|
>>> p.parents[2]
|
2013-11-17 10:33:06 -05:00
|
|
|
PureWindowsPath('c:/')
|
2012-10-05 14:19:40 -04:00
|
|
|
|
|
|
|
|
|
|
|
Querying
|
|
|
|
--------
|
|
|
|
|
|
|
|
``is_relative()`` returns True if the path is relative (see definition
|
|
|
|
above), False otherwise.
|
|
|
|
|
|
|
|
``is_reserved()`` returns True if a Windows path is a reserved path such
|
|
|
|
as ``CON`` or ``NUL``. It always returns False for POSIX paths.
|
|
|
|
|
2013-11-17 10:47:01 -05:00
|
|
|
``match()`` matches the path against a glob pattern. It operates on
|
|
|
|
individual parts and matches from the right:
|
2012-10-05 14:19:40 -04:00
|
|
|
|
2013-11-17 10:47:01 -05:00
|
|
|
>>> p = PurePosixPath('/usr/bin')
|
|
|
|
>>> p.match('/usr/b*')
|
2012-10-05 14:19:40 -04:00
|
|
|
True
|
2013-11-17 10:47:01 -05:00
|
|
|
>>> p.match('usr/b*')
|
|
|
|
True
|
|
|
|
>>> p.match('b*')
|
|
|
|
True
|
|
|
|
>>> p.match('/u*')
|
|
|
|
False
|
|
|
|
|
|
|
|
This behaviour respects the following expectations:
|
|
|
|
|
|
|
|
- A simple pattern such as "\*.py" matches arbitrarily long paths as long
|
|
|
|
as the last part matches, e.g. "/usr/foo/bar.py".
|
|
|
|
|
|
|
|
- Longer patterns can be used as well for more complex matching, e.g.
|
|
|
|
"/usr/foo/\*.py" matches "/usr/foo/bar.py".
|
2012-10-05 14:19:40 -04:00
|
|
|
|
|
|
|
|
|
|
|
Concrete paths API
|
|
|
|
==================
|
|
|
|
|
|
|
|
In addition to the operations of the pure API, concrete paths provide
|
|
|
|
additional methods which actually access the filesystem to query or mutate
|
|
|
|
information.
|
|
|
|
|
|
|
|
|
|
|
|
Constructing
|
|
|
|
------------
|
|
|
|
|
|
|
|
The classmethod ``cwd()`` creates a path object pointing to the current
|
|
|
|
working directory in absolute form::
|
|
|
|
|
|
|
|
>>> Path.cwd()
|
|
|
|
PosixPath('/home/antoine/pathlib')
|
|
|
|
|
|
|
|
|
|
|
|
File metadata
|
|
|
|
-------------
|
|
|
|
|
2013-11-04 10:22:03 -05:00
|
|
|
The ``stat()`` returns the file's stat() result; similarly, ``lstat()``
|
|
|
|
returns the file's lstat() result (which is different iff the file is a
|
|
|
|
symbolic link)::
|
2012-10-05 14:19:40 -04:00
|
|
|
|
|
|
|
>>> p.stat()
|
|
|
|
posix.stat_result(st_mode=33277, st_ino=7483155, st_dev=2053, st_nlink=1, st_uid=500, st_gid=500, st_size=928, st_atime=1343597970, st_mtime=1328287308, st_ctime=1343597964)
|
|
|
|
|
|
|
|
Higher-level methods help examine the kind of the file::
|
|
|
|
|
|
|
|
>>> p.exists()
|
|
|
|
True
|
|
|
|
>>> p.is_file()
|
|
|
|
True
|
|
|
|
>>> p.is_dir()
|
|
|
|
False
|
|
|
|
>>> p.is_symlink()
|
|
|
|
False
|
2013-11-19 10:52:03 -05:00
|
|
|
>>> p.is_socket()
|
2013-11-18 15:49:41 -05:00
|
|
|
False
|
|
|
|
>>> p.is_fifo()
|
|
|
|
False
|
|
|
|
>>> p.is_block_device()
|
|
|
|
False
|
|
|
|
>>> p.is_char_device()
|
|
|
|
False
|
2012-10-05 14:19:40 -04:00
|
|
|
|
|
|
|
The file owner and group names (rather than numeric ids) are queried
|
2013-11-18 17:49:02 -05:00
|
|
|
through corresponding methods::
|
2012-10-05 14:19:40 -04:00
|
|
|
|
|
|
|
>>> p = Path('/etc/shadow')
|
2013-11-18 17:49:02 -05:00
|
|
|
>>> p.owner()
|
2012-10-05 14:19:40 -04:00
|
|
|
'root'
|
2013-11-18 17:49:02 -05:00
|
|
|
>>> p.group()
|
2012-10-05 14:19:40 -04:00
|
|
|
'shadow'
|
|
|
|
|
|
|
|
|
|
|
|
Path resolution
|
|
|
|
---------------
|
|
|
|
|
|
|
|
The ``resolve()`` method makes a path absolute, resolving any symlink on
|
2013-11-17 10:49:07 -05:00
|
|
|
the way (like the POSIX realpath() call). It is the only operation which
|
2013-11-18 15:48:50 -05:00
|
|
|
will remove "``..``" path components. On Windows, this method will also
|
|
|
|
take care to return the canonical path (with the right casing).
|
2012-10-05 14:19:40 -04:00
|
|
|
|
|
|
|
|
|
|
|
Directory walking
|
|
|
|
-----------------
|
|
|
|
|
2013-11-16 14:08:09 -05:00
|
|
|
Simple (non-recursive) directory access is done by calling the iterdir()
|
|
|
|
method, which returns an iterator over the child paths::
|
2012-10-05 14:19:40 -04:00
|
|
|
|
|
|
|
>>> p = Path('docs')
|
2013-11-16 14:08:09 -05:00
|
|
|
>>> for child in p.iterdir(): child
|
2012-10-05 14:19:40 -04:00
|
|
|
...
|
|
|
|
PosixPath('docs/conf.py')
|
|
|
|
PosixPath('docs/_templates')
|
|
|
|
PosixPath('docs/make.bat')
|
|
|
|
PosixPath('docs/index.rst')
|
|
|
|
PosixPath('docs/_build')
|
|
|
|
PosixPath('docs/_static')
|
|
|
|
PosixPath('docs/Makefile')
|
|
|
|
|
|
|
|
This allows simple filtering through list comprehensions::
|
|
|
|
|
|
|
|
>>> p = Path('.')
|
2013-11-16 14:08:09 -05:00
|
|
|
>>> [child for child in p.iterdir() if child.is_dir()]
|
2012-10-05 14:19:40 -04:00
|
|
|
[PosixPath('.hg'), PosixPath('docs'), PosixPath('dist'), PosixPath('__pycache__'), PosixPath('build')]
|
|
|
|
|
|
|
|
Simple and recursive globbing is also provided::
|
|
|
|
|
|
|
|
>>> for child in p.glob('**/*.py'): child
|
|
|
|
...
|
|
|
|
PosixPath('test_pathlib.py')
|
|
|
|
PosixPath('setup.py')
|
|
|
|
PosixPath('pathlib.py')
|
|
|
|
PosixPath('docs/conf.py')
|
|
|
|
PosixPath('build/lib/pathlib.py')
|
|
|
|
|
|
|
|
|
|
|
|
File opening
|
|
|
|
------------
|
|
|
|
|
|
|
|
The ``open()`` method provides a file opening API similar to the builtin
|
|
|
|
``open()`` method::
|
|
|
|
|
|
|
|
>>> p = Path('setup.py')
|
|
|
|
>>> with p.open() as f: f.readline()
|
|
|
|
...
|
|
|
|
'#!/usr/bin/env python3\n'
|
|
|
|
|
|
|
|
|
2013-10-25 15:09:57 -04:00
|
|
|
Filesystem modification
|
|
|
|
-----------------------
|
2012-10-05 14:19:40 -04:00
|
|
|
|
|
|
|
Several common filesystem operations are provided as methods: ``touch()``,
|
|
|
|
``mkdir()``, ``rename()``, ``replace()``, ``unlink()``, ``rmdir()``,
|
|
|
|
``chmod()``, ``lchmod()``, ``symlink_to()``. More operations could be
|
|
|
|
provided, for example some of the functionality of the shutil module.
|
|
|
|
|
2013-11-16 13:57:44 -05:00
|
|
|
Detailed documentation of the proposed API can be found at the `pathlib
|
|
|
|
docs`_.
|
|
|
|
|
|
|
|
.. _pathlib docs: https://pathlib.readthedocs.org/en/pep428/
|
|
|
|
|
2012-10-05 14:19:40 -04:00
|
|
|
|
2013-10-25 15:28:38 -04:00
|
|
|
Discussion
|
|
|
|
==========
|
|
|
|
|
|
|
|
Division operator
|
|
|
|
-----------------
|
|
|
|
|
|
|
|
The division operator came out first in a `poll`_ about the path joining
|
|
|
|
operator. Initial versions of `pathlib`_ used square brackets
|
|
|
|
(i.e. ``__getitem__``) instead.
|
|
|
|
|
|
|
|
.. _poll: https://mail.python.org/pipermail/python-ideas/2012-October/016544.html
|
|
|
|
|
|
|
|
joinpath()
|
|
|
|
----------
|
|
|
|
|
|
|
|
The joinpath() method was initially called join(), but several people
|
|
|
|
objected that it could be confused with str.join() which has different
|
|
|
|
semantics. Therefore it was renamed to joinpath().
|
|
|
|
|
|
|
|
Case-sensitivity
|
|
|
|
----------------
|
|
|
|
|
|
|
|
Windows users consider filesystem paths to be case-insensitive and expect
|
|
|
|
path objects to observe that characteristic, even though in some rare
|
|
|
|
situations some foreign filesystem mounts may be case-sensitive under
|
|
|
|
Windows.
|
|
|
|
|
|
|
|
In the words of one commenter,
|
|
|
|
|
|
|
|
"If glob("\*.py") failed to find SETUP.PY on Windows, that would be a
|
|
|
|
usability disaster".
|
|
|
|
|
|
|
|
-- Paul Moore in
|
|
|
|
https://mail.python.org/pipermail/python-dev/2013-April/125254.html
|
|
|
|
|
|
|
|
|
2012-10-05 14:19:40 -04:00
|
|
|
Copyright
|
|
|
|
=========
|
|
|
|
|
|
|
|
This document has been placed into the public domain.
|
|
|
|
|
|
|
|
|
|
|
|
..
|
|
|
|
Local Variables:
|
|
|
|
mode: indented-text
|
|
|
|
indent-tabs-mode: nil
|
|
|
|
sentence-end-double-space: t
|
|
|
|
fill-column: 70
|
|
|
|
coding: utf-8
|