def _rm(self, path): if _share_has_path(path): wpath = _as_unc_path(self.host, path) stats = smbclient.stat(wpath) if S_ISDIR(stats.st_mode): smbclient.rmdir(wpath) else: smbclient.remove(wpath)
def smb_remove_file(client: SMBClient, args: dict): hostname = args.get('hostname') path = handle_path(args.get('file_path')) path = os.path.join(hostname or client.hostname, path) username = args.get('username') password = args.get('password') client.create_session(hostname, username, password) remove(path) file_name = get_file_name(path) return f'File {file_name} was deleted successfully'
def remove(self, path): return smbclient.remove(self._join_path(path), **self._conn_kwargs)
def discard(self): """Remove the temp file on failure.""" smbclient.remove(self.temp)
# Optional - register the server with explicit credentials register_session("server", username="******", password="******") # Read an existing file as text (credentials only needed for the first request to the server if not registered.) with open_file(r"\\server\share\file.txt", username="******", password="******") as fd: file_contents = fd.read() # Read an existing file as bytes with open_file(r"\\server\share\file.txt", mode="rb") as fd: file_bytes = fd.read() # Create a file and write to it with open_file(r"\\server\share\file.txt", mode="w") as fd: fd.write(u"content") # Write data to the end of an existing file with open_file(r"\\server\share\file.txt", mode="a") as fd: fd.write(u"\ndata at the end") # Delete a file remove(r"\\server\share\file.txt") # Get info about a file stat(r"\\server\share\file.txt") # Create a symbolic link symlink(r"\\server\share\directory", r"\\server\share\link") # Create a hard link link(r"\\server\share\file.txt", r"\\server\share\hard-link.txt")