Python 3.x 将文件从SFTP服务器下载到本地目录

Python 3.x 将文件从SFTP服务器下载到本地目录,python-3.x,pysftp,Python 3.x,Pysftp,我得到了一个使用python将文件从远程目录下载到本地目录的代码。我想要一个代码,可以下载一个单一的文件从远程目录到本地文件夹。请帮我修改这个代码。欢迎所有建议。 我测试了这段代码,但它将远程目录的所有内容下载到本地目录,但我想要一段代码,可以将单个文件从远程目录下载到本地目录。我正在使用SFTP服务器 我得到的代码:(基于) 您需要删除sftp.listdir(remotedir)中的条目,只需使用sftp.get(remotepath,localpath,preserve\u mtime=p

我得到了一个使用python将文件从远程目录下载到本地目录的代码。我想要一个代码,可以下载一个单一的文件从远程目录到本地文件夹。请帮我修改这个代码。欢迎所有建议。 我测试了这段代码,但它将远程目录的所有内容下载到本地目录,但我想要一段代码,可以将单个文件从远程目录下载到本地目录。我正在使用SFTP服务器

我得到的代码:(基于)


您需要删除sftp.listdir(remotedir)中的
条目
,只需使用
sftp.get(remotepath,localpath,preserve\u mtime=preserve\u mtime)
如果可以,请在TonyJafar修改代码。我是Python新手您需要删除sftp.listdir(remotedir)中的
条目
,只需使用
sftp.get(remotepath,localpath,preserve\u mtime=preserve\u mtime)
如果您觉得舒服,请修改代码@TonyJafar。我是pythonI的新手,我一直在尝试使用这个解决方案来满足我的需求,我不断得到“SSHException:Server connection drop:“have try channel=sftp.sftp\u client.get\u channel()channel.lock.acquire()channel.default\u max\u packet\u size=paramiko.common.default\u max\u packet\u size,channel.out_window_size=paramiko.common.MAX_window_size channel.transport.packetizer.REKEY_BYTES=pow(2,40)#1TB MAX,这是一种安全降级!channel.transport.packetizer.REKEY_PACKETS=pow(2,40)channel.lock.release()“但似乎没有任何效果-请提供一些帮助?我一直在尝试根据自己的需要使用此解决方案,我不断得到“sHexception:Server connection drop:”已尝试channel=sftp.sftp_client.get_channel()channel.lock.acquire()channel.default\u max\u packet\u size=paramiko.common.default\u max\u packet\u size,channel.out\u window\u size=paramiko.common.max\u window\u size channel.transport.packetizer.REKEY\u BYTES=pow(2,40)max,这是一种安全性降级!channel.transport.packetizer.REKEY\u packetizer=pow(2,40)channel.lock.release()但似乎什么都不管用-请帮忙?
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,mode=777)
            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)
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, remotefile, localdir, preserve_mtime=False):
    remotepath = remotedir + "/" + remotefile
    localpath = os.path.join(localdir, remotefile)
     mode = sftp.stat(remotepath).st_mode
     if S_ISDIR(mode):
          try:
              os.mkdir(localpath,mode=777)
          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: ")
remote_file=input("enter the remote file: ")

local_path=input("enter the local_path: ")

get_r_portable(sftp, remote_path, remote_file, local_path, preserve_mtime=False)