0

I got a code to download files from remote directory to local directory using python. I want a code that can download can a single file from remote directory to local folder. Please help me to modify this code. All suggestions are welcomed. I tested this code but it download all the contents of remote directory to local directory,but i want a code that can download a single file from remote to local directory. I am using SFTP server

code i got: (based on Python pysftp get_r from Linux works fine on Linux but not on Windows)

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)
  • you need to remove `for entry in sftp.listdir(remotedir)` and just use `sftp.get(remotepath, localpath, preserve_mtime=preserve_mtime)` – LinPy May 29 '19 at 06:11
  • if comfortable can you please modify the code @TonyJafar . I am new to python – simon cowell May 29 '19 at 06:17

1 Answers1

2
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)
LinPy
  • 16,987
  • 4
  • 43
  • 57
  • I have been trying to use this solution for my needs and I keep getting "SSHException: Server connection dropped: " Have tried channel = sftp.sftp_client.get_channel() channel.lock.acquire() channel.default_max_packet_size=paramiko.common.DEFAULT_MAX_PACKET_SIZE, channel.out_window_size=paramiko.common.MAX_WINDOW_SIZE channel.transport.packetizer.REKEY_BYTES = pow(2, 40) # 1TB max, this is a security degradation! channel.transport.packetizer.REKEY_PACKETS = pow(2, 40) channel.lock.release()" but nothing seems to work - some help please? – Stumbling Through Data Science Jun 25 '19 at 14:38