SFTP:从远程服务器递归中复制/下载所有文件



我得到了一个可以从远程目录下载到本地目录的代码,但是它给出了以下错误。

请解释错误的含义以及如何解决。它显示了第二行的问题。

我的代码

import paramiko, os
paramiko.util.log_to_file('/tmp/paramiko.log')
from stat import S_ISDIR
host = "ip"
port = 22
transport = paramiko.Transport((host, port))
password = "mypassword"
username = "username"
transport.connect(username = username, password = password)
sftp = paramiko.SFTPClient.from_transport(transport)
def sftp_walk(remotepath):
    path=remotepath
    files=[]
    folders=[]
    for f in sftp.listdir_attr(remotepath):
        if S_ISDIR(f.st_mode):
            folders.append(f.filename)
        else:
            files.append(f.filename)
    if files:
        yield path, files
    for folder in folders:
        new_path=os.path.join(remotepath,folder)
        for x in sftp_walk(new_path):
            yield x

for path,files  in sftp_walk("." or '/remotepath/'):
    for file in files:
        #sftp.get(remote, local) line for dowloading.
        sftp.get(os.path.join(os.path.join(path,file)), '/local path/')

错误我正在得到:

C:UsersRohanPycharmProjectsuntitled1venvScriptspython.exe C:/Users/Rohan/PycharmProjects/untitled1/tyu.py
Traceback (most recent call last):
  File "C:/Users/Rohan/PycharmProjects/untitled1/tyu.py", line 2, in <module>
    paramiko.util.log_to_file('/tmp/paramiko.log')
  File "C:UsersRohanPycharmProjectsuntitled1venvlibsite-packagesparamikoutil.py", line 252, in log_to_file
    f = open(filename, "a")
FileNotFoundError: [Errno 2] No such file or directory: '/tmp/paramiko.log'
Process finished with exit code 1
import os
import pysftp
from stat import S_IMODE, S_ISDIR, S_ISREG
cnopts = pysftp.CnOpts()
cnopts.hostkeys = None    
sftp=pysftp.Connection('192.168.X.X', username='username',password='password',cnopts=cnopts)
def get_r_portable(sftp, remotedir, localdir, preserve_mtime=False):
    for entry in sftp.listdir(remotedir):
        remotepath = remotedir + "/" + entry
        localpath = os.path.join(localdir, entry)
        mode = sftp.stat(remotepath).st_mode
        if S_ISDIR(mode):
            try:
                os.mkdir(localpath)
            except OSError:     
                pass
            get_r_portable(sftp, remotepath, localpath, preserve_mtime)
        elif S_ISREG(mode):
            sftp.get(remotepath, localpath, preserve_mtime=preserve_mtime)
remote_path=input("enter the remote_path: ")
local_path=input("enter the local_path: ")
get_r_portable(sftp, remote_path, local_path, preserve_mtime=False)

最新更新