Package plumbum.path¶
- class plumbum.path.base.FSUser(val, name=None)[source]¶
A special object that represents a file-system user. It derives from
int, so it behaves just like a number (uid/gid), but also have a.nameattribute that holds the string-name of the user, if given (otherwiseNone)
- class plumbum.path.base.Path[source]¶
An abstraction over file system paths. This class is abstract, and the two implementations are
LocalPathandRemotePath.- __rtruediv__(other)[source]¶
Joins two paths when this path appears on the right-hand side.
- Return type:
Self
- __getitem__(key)[source]¶
- Overloads:
self, key (str | Path) → Self
self, key (SupportsIndex | slice) → str
Return self[key].
- __floordiv__(expr)[source]¶
Returns a (possibly empty) list of paths that matched the glob-pattern under this path
- Return type:
list[Self]
- __contains__(item)[source]¶
Paths should support checking to see if an file or folder is in them.
- Return type:
- joinpath(*others)[source]¶
Pathlib-compatible alias of
join().Added in version 2.0.
- Return type:
Self
- walk(filter=<function Path.<lambda>>, dir_filter=<function Path.<lambda>>)[source]¶
traverse all (recursive) sub-elements under this directory, that match the given filter. By default, the filter accepts everything; you can provide a custom filter function that takes a path as an argument and returns a boolean
- Parameters:
filter (
Callable[[Self],bool]) – the filter (predicate function) for matching results. Only paths matching this predicate are returned. Defaults to everything.dir_filter (
Callable[[Self],bool]) – the filter (predicate function) for matching directories. Only directories matching this predicate are recursed into. Defaults to everything.
- Return type:
- abstract property name¶
The basename component of this path
- abstract property stem¶
The name without an extension, or the last component of the path
- abstract property dirname¶
The dirname component of this path
- abstract property root¶
The root of the file tree (/ on Unix)
- abstract property drive¶
The drive letter (on Windows)
- abstract property suffix¶
The suffix of this file
- abstract property suffixes¶
This is a list of all suffixes
- abstract property uid¶
The user that owns this path. The returned value is a
FSUserobject which behaves like anint(as expected fromuid), but it also has a.nameattribute that holds the string-name of the user
- abstract property gid¶
The group that owns this path. The returned value is a
FSUserobject which behaves like anint(as expected fromgid), but it also has a.nameattribute that holds the string-name of the group
- abstractmethod as_uri(scheme=Ellipsis)[source]¶
Returns a universal resource identifier. Use
schemeto force a scheme.- Return type:
- abstractmethod iterdir()[source]¶
Returns an iterator over the directory. Might be slightly faster on Python 3.5 than .list()
- Return type:
Iterable[Self]
- abstractmethod is_dir()[source]¶
Returns
Trueif this path is a directory,Falseotherwise- Return type:
- abstractmethod is_file()[source]¶
Returns
Trueif this path is a regular file,Falseotherwise- Return type:
- abstractmethod is_symlink()[source]¶
Returns
Trueif this path is a symbolic link,Falseotherwise- Return type:
- abstractmethod with_suffix(suffix, depth=1)[source]¶
Returns a path with the suffix replaced. Up to last
depthsuffixes will be replaced. None will replace all suffixes. If there are less thandepthsuffixes, this will replace all suffixes..tar.gzis an example wheredepth=2ordepth=Noneis useful- Return type:
Self
- preferred_suffix(suffix)[source]¶
Adds a suffix if one does not currently exist (otherwise, no change). Useful for loading files with a default suffix
- Return type:
Self
- with_stem(stem)[source]¶
Returns a path with the stem replaced.
Added in version 2.0.
- Return type:
Self
- abstractmethod glob(pattern)[source]¶
Returns a (possibly empty) list of paths that matched the glob-pattern under this path
- Return type:
list[Self]
- rename(newname)[source]¶
Renames this path to the
new name(only the basename is changed)- Return type:
Self
- abstractmethod copy(dst, override=None)[source]¶
Copies this path (recursively, if a directory) to the destination path “dst”. Raises TypeError if dst exists and override is False. Will overwrite if override is True. Will silently fail to copy if override is None (the default).
- Return type:
Self
- abstractmethod mkdir(mode=511, parents=True, exist_ok=True)[source]¶
Creates a directory at this path.
- Parameters:
mode (
int) – Currently only implemented for local paths! Numeric mode to use for directory creation, which may be ignored on some systems. The current implementation reproduces the behavior ofos.mkdir(i.e., the current umask is first masked out), but this may change for remote paths. As withos.mkdir, it is recommended to callchmod()explicitly if you need to be sure.parents (
bool) – If this is true (the default), the directory’s parents will also be created if necessary.exist_ok (
bool) – If this is true (the default), no exception will be raised if the directory already exists (otherwiseOSError).
- Return type:
Note that the defaults for
parentsandexist_okare the opposite of what they are in Python’s ownpathlib- this is to maintain backwards-compatibility with Plumbum’s behaviour from before they were implemented.
- read_bytes()[source]¶
Returns the contents of this file as
bytes.Added in version 2.0.
- Return type:
- read_text(encoding=None, errors=None)[source]¶
Returns the contents of this file as
str.Added in version 2.0.
- Return type:
- abstractmethod read(encoding=None)[source]¶
returns the contents of this file as a
str. By default the data is read as text, but you can specify the encoding, e.g.,'latin1'or'utf8'
- abstractmethod write(data, encoding=None)[source]¶
writes the given data to this file. By default the data is written as-is (either text or binary), but you can specify the encoding, e.g.,
'latin1'or'utf8'- Return type:
- write_bytes(data)[source]¶
Writes bytes to this file and returns the number of bytes written.
Added in version 2.0.
- Return type:
- write_text(data, encoding=None, errors=None, newline=None)[source]¶
Writes text to this file and returns the number of characters written.
Added in version 2.0.
- Return type:
- abstractmethod touch()[source]¶
Update the access time. Creates an empty file if none exists.
- Return type:
- abstractmethod chown(owner=None, group=None, recursive=None)[source]¶
Change ownership of this path.
- Parameters:
owner (
int|str|None) – The owner to set (eitheruidorusername), optionalgroup (
int|str|None) – The group to set (eithergidorgroupname), optionalrecursive (
bool|None) – whether to change ownership of all contained files and subdirectories. Only meaningful whenselfis a directory. IfNone, the value will default toTrueifselfis a directory,Falseotherwise.
- Return type:
- symlink_to(target)[source]¶
Pathlib-compatible symbolic-link creation.
Creates a symbolic link at
selfthat points totarget.Added in version 2.0.
- Return type:
- hardlink_to(target)[source]¶
Pathlib-compatible hard-link creation.
Creates a hard link at
selfthat points totarget.Added in version 2.0.
- Return type:
- lstat()[source]¶
Like
stat(). Backends may override for symlink-specific behavior.Added in version 2.0.
- Return type:
- split(*_args, **_kargs)[source]¶
Splits the path on directory separators, yielding a list of directories, e.g,
"/var/log/messages"will yield['var', 'log', 'messages'].
- property parts¶
Splits the directory into parts, including the base directory, returns a tuple
- property anchor¶
Pathlib-compatible anchor (drive + root).
Added in version 2.0.
- as_posix()[source]¶
Returns the path string with POSIX separators.
Added in version 2.0.
- Return type:
- absolute()[source]¶
Returns an absolute version of this path.
Added in version 2.0.
- Return type:
Self
- is_relative_to(other)[source]¶
Returns
Truewhen this path is withinother.Added in version 2.0.
- Return type:
- samefile(other)[source]¶
Returns
Trueif this path andotherpoint to the same file.Added in version 2.0.
- Return type:
- match(pattern)[source]¶
Tests if this path matches a glob-style pattern.
Added in version 2.0.
- Return type:
- rglob(pattern)[source]¶
Recursively glob under this path.
Added in version 2.0.
- Return type:
list[Self]
- relative_to(source)[source]¶
Computes the “relative path” require to get from
sourcetoself. They satisfy the invariantsource_path + (target_path - source_path) == target_path. For example:/var/log/messages - /var/log/messages = [] /var/log/messages - /var = [log, messages] /var/log/messages - / = [var, log, messages] /var/log/messages - /var/tmp = [.., log, messages] /var/log/messages - /opt = [.., var, log, messages] /var/log/messages - /opt/lib = [.., .., var, log, messages]
- Return type:
- resolve(strict=False)[source]¶
Added to allow pathlib like syntax. Does nothing since Plumbum paths are always absolute. Does not (currently) resolve symlinks.
- Return type:
Self
- property parents¶
Pathlib like sequence of ancestors
- property parent¶
Pathlib like parent of the path.
- class plumbum.path.base.RelativePath(parts)[source]¶
Relative paths are the “delta” required to get from one path to another. Note that relative path do not point at anything, and thus are not paths. Therefore they are system agnostic (but closed under addition) Paths are always absolute and point at “something”, whether existent or not.
Relative paths are created by subtracting paths (
Path.relative_to)
- class plumbum.path.local.LocalPath(*parts)[source]¶
The class implementing local-machine paths
- property name¶
The basename component of this path
- property dirname¶
The dirname component of this path
- property suffix¶
The suffix of this file
- property suffixes¶
This is a list of all suffixes
- property uid¶
The user that owns this path. The returned value is a
FSUserobject which behaves like anint(as expected fromuid), but it also has a.nameattribute that holds the string-name of the user
- property gid¶
The group that owns this path. The returned value is a
FSUserobject which behaves like anint(as expected fromgid), but it also has a.nameattribute that holds the string-name of the group
- iterdir()[source]¶
Returns an iterator over the directory. Might be slightly faster on Python 3.5 than .list()
- property stem¶
The name without an extension, or the last component of the path
- with_suffix(suffix, depth=1)[source]¶
Returns a path with the suffix replaced. Up to last
depthsuffixes will be replaced. None will replace all suffixes. If there are less thandepthsuffixes, this will replace all suffixes..tar.gzis an example wheredepth=2ordepth=Noneis useful- Return type:
- glob(pattern)[source]¶
Returns a (possibly empty) list of paths that matched the glob-pattern under this path
- copy(dst, override=None)[source]¶
Copies this path (recursively, if a directory) to the destination path “dst”. Raises TypeError if dst exists and override is False. Will overwrite if override is True. Will silently fail to copy if override is None (the default).
- Return type:
- mkdir(mode=511, parents=True, exist_ok=True)[source]¶
Creates a directory at this path.
- Parameters:
mode (
int) – Currently only implemented for local paths! Numeric mode to use for directory creation, which may be ignored on some systems. The current implementation reproduces the behavior ofos.mkdir(i.e., the current umask is first masked out), but this may change for remote paths. As withos.mkdir, it is recommended to callchmod()explicitly if you need to be sure.parents (
bool) – If this is true (the default), the directory’s parents will also be created if necessary.exist_ok (
bool) – If this is true (the default), no exception will be raised if the directory already exists (otherwiseOSError).
- Return type:
Note that the defaults for
parentsandexist_okare the opposite of what they are in Python’s ownpathlib- this is to maintain backwards-compatibility with Plumbum’s behaviour from before they were implemented.
- read(encoding=None, mode='r')[source]¶
returns the contents of this file as a
str. By default the data is read as text, but you can specify the encoding, e.g.,'latin1'or'utf8'
- write(data, encoding=None, mode=None)[source]¶
writes the given data to this file. By default the data is written as-is (either text or binary), but you can specify the encoding, e.g.,
'latin1'or'utf8'- Return type:
- chown(owner=None, group=None, recursive=None)[source]¶
Change ownership of this path.
- Parameters:
owner (
int|str|None) – The owner to set (eitheruidorusername), optionalgroup (
int|str|None) – The group to set (eithergidorgroupname), optionalrecursive (
bool|None) – whether to change ownership of all contained files and subdirectories. Only meaningful whenselfis a directory. IfNone, the value will default toTrueifselfis a directory,Falseotherwise.
- Return type:
- as_uri(scheme='file')[source]¶
Returns a universal resource identifier. Use
schemeto force a scheme.- Return type:
- property drive¶
The drive letter (on Windows)
- property root¶
The root of the file tree (/ on Unix)
- class plumbum.path.local.LocalWorkdir[source]¶
Working directory manipulator
- __hash__ = None¶
- chdir(newdir)[source]¶
Changes the current working directory to the given one
- class plumbum.path.remote.RemotePath(remote, *parts_str)[source]¶
The class implementing remote-machine paths
- property name¶
The basename component of this path
- property dirname¶
The dirname component of this path
- property suffix¶
The suffix of this file
- property suffixes¶
This is a list of all suffixes
- property uid¶
The user that owns this path. The returned value is a
FSUserobject which behaves like anint(as expected fromuid), but it also has a.nameattribute that holds the string-name of the user
- property gid¶
The group that owns this path. The returned value is a
FSUserobject which behaves like anint(as expected fromgid), but it also has a.nameattribute that holds the string-name of the group
- iterdir()[source]¶
Returns an iterator over the directory. Might be slightly faster on Python 3.5 than .list()
- Return type:
- with_suffix(suffix, depth=1)[source]¶
Returns a path with the suffix replaced. Up to last
depthsuffixes will be replaced. None will replace all suffixes. If there are less thandepthsuffixes, this will replace all suffixes..tar.gzis an example wheredepth=2ordepth=Noneis useful- Return type:
- glob(pattern)[source]¶
Returns a (possibly empty) list of paths that matched the glob-pattern under this path
- Return type:
- copy(dst, override=False)[source]¶
Copies this path (recursively, if a directory) to the destination path “dst”. Raises TypeError if dst exists and override is False. Will overwrite if override is True. Will silently fail to copy if override is None (the default).
- Return type:
- mkdir(mode=None, parents=True, exist_ok=True)[source]¶
Creates a directory at this path.
- Parameters:
mode (
int|None) – Currently only implemented for local paths! Numeric mode to use for directory creation, which may be ignored on some systems. The current implementation reproduces the behavior ofos.mkdir(i.e., the current umask is first masked out), but this may change for remote paths. As withos.mkdir, it is recommended to callchmod()explicitly if you need to be sure.parents (
bool) – If this is true (the default), the directory’s parents will also be created if necessary.exist_ok (
bool) – If this is true (the default), no exception will be raised if the directory already exists (otherwiseOSError).
- Return type:
Note that the defaults for
parentsandexist_okare the opposite of what they are in Python’s ownpathlib- this is to maintain backwards-compatibility with Plumbum’s behaviour from before they were implemented.
- read(encoding=None)[source]¶
returns the contents of this file as a
str. By default the data is read as text, but you can specify the encoding, e.g.,'latin1'or'utf8'
- write(data, encoding=None)[source]¶
writes the given data to this file. By default the data is written as-is (either text or binary), but you can specify the encoding, e.g.,
'latin1'or'utf8'- Return type:
- chown(owner=None, group=None, recursive=None)[source]¶
Change ownership of this path.
- Parameters:
owner (
int|str|None) – The owner to set (eitheruidorusername), optionalgroup (
int|str|None) – The group to set (eithergidorgroupname), optionalrecursive (
bool|None) – whether to change ownership of all contained files and subdirectories. Only meaningful whenselfis a directory. IfNone, the value will default toTrueifselfis a directory,Falseotherwise.
- Return type:
- link(dst)[source]¶
Creates a hard link from
selftodst- Parameters:
dst (
RemotePath|str) – the destination path- Return type:
- symlink(dst)[source]¶
Creates a symbolic link from
selftodst- Parameters:
dst (
RemotePath|str) – the destination path- Return type:
- open(mode='r', bufsize=-1, *, encoding=None)[source]¶
Opens this path as a file.
Only works for ParamikoMachine-associated paths for now.
- as_uri(scheme='ssh')[source]¶
Returns a universal resource identifier. Use
schemeto force a scheme.- Return type:
- property stem¶
The name without an extension, or the last component of the path
- property root¶
The root of the file tree (/ on Unix)
- property drive¶
The drive letter (on Windows)
- class plumbum.path.remote.RemoteWorkdir(remote)[source]¶
Remote working directory manipulator
- __hash__ = None¶
- getpath()[source]¶
Returns the current working directory as a remote path <plumbum.path.remote.RemotePath> object
- Return type:
- __call__(newdir)[source]¶
A context manager used to
chdirinto a directory and thenchdirback to the previous location; much likepushd/popd.- Parameters:
newdir (
RemotePath|str) – The destination director (a string or aRemotePath)- Return type:
Utils¶
- plumbum.path.utils.copy(src, dst)[source]¶
Copy (recursively) the source path onto the destination path;
srcanddstcan be either strings,LocalPathsorRemotePath; any combination of the three will work.Added in version 1.3:
srccan also be a list of strings/paths, in which casedstmust not exist or be a directory.- Return type:
- plumbum.path.utils.delete(*paths)[source]¶
Deletes the given paths. The arguments can be either strings,
local paths,remote paths, or iterables of such. No error is raised if any of the paths does not exist (it is silently ignored)- Return type:
- plumbum.path.utils.gui_open(filename)[source]¶
This selects the proper gui open function. This can also be achieved with webbrowser, but that is not supported.
- Return type:
- plumbum.path.utils.move(src, dst)[source]¶
Moves the source path onto the destination path;
srcanddstcan be either strings,LocalPathsorRemotePath; any combination of the three will work.Added in version 1.3:
srccan also be a list of strings/paths, in which casedstmust not exist or be a directory.- Return type: