我想删除已连接到的远程服务器上给定目录中的所有文件,使用Paramiko。然而,我无法显式地给出文件名,因为这将取决于我之前放置在那里的文件版本。
以下是我正在尝试的内容... #TODO下面的行是我尝试的调用,其中remoteArtifactPath
类似于/opt/foo/*
ssh = paramiko.SSHClient()
ssh.load_host_keys(os.path.expanduser(os.path.join("~", ".ssh", "known_hosts")))
ssh.connect(server, username=username, pkey=mykey)
sftp = ssh.open_sftp()
# TODO: Need to somehow delete all files in remoteArtifactPath remotely
sftp.remove(remoteArtifactPath+"*")
# Close to end
sftp.close()
ssh.close()
有什么想法可以实现这个吗?
os.path.join(remoteArtifactPath, file)
代替sftp.remove(remoteArtifactPath+file)
, 因为os.path.join()
是跨平台的。行分隔符在不同平台上可能不同,使用os.path.join
可以确保生成正确的路径,而不受平台影响。 - 9monkeys