包含文件集的目录,包含文件的子目录和子目录,我需要检查这些文件是否确实 在远程位置生成。
说[a.log,b.log,c.log,tmp_folder]
和tmp_folder
包含d.log,e.log
个文件
当前,我正在登录远程计算机并获取文件列表,但无法匹配这些文件,也无法将整个目录sftp到本地计算机。
remote_dir = "some/location/path/time_stamp_10101010"
local_dir = "location/on/local/path"
需要将整个目录time_stamp_10101010
复制到local_path
中。
import re,os,sys
import paramiko
remote_dir = "some/location/path/time_stamp_10101010"
local_dir = "location/on/local/path"
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(host_ip, username=username,key_filename=key_filename,port=22)
stdin, stdout, stderr = ssh.exec_command("ls")
sftp_client = ssh.open_sftp()
dir_items = sftp_client.listdir_attr(remote_dir)
for item in dir_items:
print item
if os.path.isfile(os.path.join(remote_dir, item)):
remote_path = remote_dir + '/' + item.filename
local_path = os.path.join(local_dir, item.filename)
sftp_client.put_dir(remote_path, local_path)
# If item is a directory here and if it contains sub-directory and files
# How can I implement to copy recursively till all sub-directories and files are included.
我做对了吗?还是有更好的方法? 如何验证目录中生成的文件,子目录是否与标准集匹配(如上所述)?我觉得reg-ex会更好。
答案 0 :(得分:0)
您可以像这样使用递归函数:
def recursive_walk(path, remote_directory="", local_directory=""):
if os.path.isfile(path):
print("I am a file take me anywhere", path)
return
for item in os.listdir(path):
item_path = os.path.join(path, item)
recursive_walk(item_path)
recursive_walk('.')