背景🎈🎈🎈:
判断远程服务器指定路径下是否有test文件夹,如果有,遍历文件夹下的所有文件,删除文件名字中有"data"字符串的所有文件。
代码如下:
- import paramiko
- import stat
-
- def run(host, port, username, password):
- ssh_client = paramiko.SSHClient()
- ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy)
- ssh_client.connect(host, port, username, password)
- ftp_client = ssh_client.open_sftp()
- Remotedirectory = ftp_client.listdir(path="/data")
- for remote_direct in Remotedirectory:
- if "test" in remote_direct:
- fileattr = ftp_client.lstat("/data/test")
- if stat.S_ISDIR(fileattr.st_mode):
- filesname = ftp_client.listdir(path="/data/test")
- for file_name in filesname:
- if "data" in file_name:
- print("delete