class pathlib.PurePath(*pathsegments)
A generic class that represents the system’s path flavour (instantiating it creates either a PurePosixPath
or a PureWindowsPath
):
1 2 | >>> PurePath( 'setup.py' ) # Running on a Unix machine PurePosixPath( 'setup.py' ) |
Each element of pathsegments can be either a string representing a path segment, or another path object:
1 2 3 4 | >>> PurePath( 'foo' , 'some/path' , 'bar' ) PurePosixPath( 'foo/some/path/bar' ) >>> PurePath(Path( 'foo' ), Path( 'bar' )) PurePosixPath( 'foo/bar' ) |
When pathsegments is empty, the current directory is assumed:
1 2 | >>> PurePath() PurePosixPath( '.' ) |
When several absolute paths are given, the last is taken as an anchor (mimicking os.path.join()
‘s behaviour):
1 2 3 4 | >>> PurePath( '/etc' , '/usr' , 'lib64' ) PurePosixPath( '/usr/lib64' ) >>> PureWindowsPath( 'c:/Windows' , 'd:bar' ) PureWindowsPath( 'd:bar' ) |
However, in a Windows path, changing the local root doesn’t discard the previous drive setting:
1 2 | >>> PureWindowsPath( 'c:/Windows' , '/Program Files' ) PureWindowsPath( 'c:/Program Files' ) |
Spurious slashes and single dots are collapsed, but double dots ('..'
) are not, since this would change the meaning of a path in the face of symbolic links:
1 2 3 4 5 6 | >>> PurePath( 'foo//bar' ) PurePosixPath( 'foo/bar' ) >>> PurePath( 'foo/./bar' ) PurePosixPath( 'foo/bar' ) >>> PurePath( 'foo/../bar' ) PurePosixPath( 'foo/../bar' ) |
(a naïve approach would make PurePosixPath('foo/../bar')
equivalent to PurePosixPath('bar')
, which is wrong if foo
is a symbolic link to another directory)
Please login to continue.