diff --git a/src/SSHLibrary/client.py b/src/SSHLibrary/client.py index cf5cb4340..9d760c70a 100644 --- a/src/SSHLibrary/client.py +++ b/src/SSHLibrary/client.py @@ -1532,6 +1532,29 @@ def _put_file(self, source, destination, mode, newline, path_separator, scp_pres position += len(data) self._close_remote_file(remote_file) + def remove_file(self, source, path_separator='/'): + r"""Removes file(s) from the remote host. + + :param str source: Must be the path to an existing file on the remote + machine or a glob pattern. + Glob patterns, like '*' and '?', can be used in the source, in + which case all the matching files are removed. + + :param str path_separator: The path separator used for joining the + paths on the remote host. On Windows, this must be set as `\`. + The default is `/`, which is also the default on Linux-like systems. + """ + remote_files = self._get_get_file_sources(source, path_separator) + if not remote_files: + msg = "There were no remote files matching '%s'." % source + raise SSHClientException(msg) + for src in remote_files: + self._remove_file(src) + + def _remove_file(self, remote_path): + remote_path = remote_path.encode(self._encoding) + self._client.remove(remote_path) + def _list(self, path): path = path.encode(self._encoding) for item in self._client.listdir_attr(path): diff --git a/src/SSHLibrary/library.py b/src/SSHLibrary/library.py index aba4026de..440645065 100644 --- a/src/SSHLibrary/library.py +++ b/src/SSHLibrary/library.py @@ -1997,6 +1997,19 @@ def put_file( scp_preserve_times, ) + @keyword(tags=("file",)) + def remove_file(self, source): + """Removes file(s) from the remote machine. + + ``source`` is a path on the remote machine. Both absolute paths and + paths relative to the current working directory are supported. + If the source contains wildcards explained in `glob patterns`, + all files matching it are removed. + + SCP is not supported. + """ + self._run_command(self.current.remove_file, source) + @keyword(tags=("file",)) def put_directory( self,