如何在 Python 中删除远程 SFTP 服务器上目录中的所有文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3406734/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to delete all files in directory on remote SFTP server in Python?
提问by Cuga
I'd like to delete all the files in a given directory on a remote server that I'm already connected to using Paramiko. I cannot explicitly give the file names, though, because these will vary depending on which version of file I had previously put there.
我想删除我已经使用 Paramiko 连接到的远程服务器上给定目录中的所有文件。但是,我无法明确给出文件名,因为这些名称会因我之前放置在那里的文件版本而异。
Here's what I'm trying to do... the line below the #TODO is the call I'm trying where remoteArtifactPathis something like /opt/foo/*
这就是我正在尝试做的事情......#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()
Any idea how I can achieve this?
知道我如何实现这一目标吗?
采纳答案by Cuga
I found a solution: Iterate over all the files in the remote location, then call removeon each of them:
我找到了一个解决方案:遍历远程位置的所有文件,然后调用remove每个文件:
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()
# Updated code below:
filesInRemoteArtifacts = sftp.listdir(path=remoteArtifactPath)
for file in filesInRemoteArtifacts:
sftp.remove(remoteArtifactPath+file)
# Close to end
sftp.close()
ssh.close()
回答by ianmclaury
A Fabricroutine could be as simple as this:
一个面料计划也可能是这样简单:
with cd(remoteArtifactPath):
run("rm *")
Fabric is great for executing shell commands on remote servers. Fabric actually uses Paramiko underneath, so you can use both if you need to.
Fabric 非常适合在远程服务器上执行 shell 命令。Fabric 实际上在下面使用了 Paramiko,因此如果需要,您可以同时使用两者。
回答by markolopa
You need a recursive routine since your remote directory may have subdirectories.
您需要递归例程,因为您的远程目录可能有子目录。
def rmtree(sftp, remotepath, level=0):
for f in sftp.listdir_attr(remotepath):
rpath = posixpath.join(remotepath, f.filename)
if stat.S_ISDIR(f.st_mode):
rmtree(sftp, rpath, level=(level + 1))
else:
rpath = posixpath.join(remotepath, f.filename)
print('removing %s%s' % (' ' * level, rpath))
sftp.remove(rpath)
print('removing %s%s' % (' ' * level, remotepath))
sftp.rmdir(remotepath)
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()
rmtree(sftp, remoteArtifactPath)
# Close to end
stfp.close()
ssh.close()
回答by broferek
For @markolopa answer, you need 2 imports to get it working:
对于@markolopa 答案,您需要 2 个导入才能使其正常工作:
import posixpath
from stat import S_ISDIR
回答by Allan Santos
I found a solution, using python3.7e spur 0.3.20. It is very possible that works with others versions as well.
我找到了一个解决方案,使用python3.7e splash 0.3.20。很可能也适用于其他版本。
import spur
shell = spur.SshShell( hostname="ssh_host", username="ssh_usr", password="ssh_pwd")
ssh_session = shell._connect_ssh()
ssh_session.exec_command('rm -rf /dir1/dir2/dir3')
ssh_session.close()

