3

i got a code that can download files from remote directory to local directory but it gives following error.

please explain meaning of the error and how to resolve it. it shows problem in second line.

my code

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/')

error i am getting:

C:\Users\Rohan\PycharmProjects\untitled1\venv\Scripts\python.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:\Users\Rohan\PycharmProjects\untitled1\venv\lib\site-packages\paramiko\util.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
simon brown
  • 73
  • 3
  • 7

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, 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)
Chnossos
  • 9,971
  • 4
  • 28
  • 40
udit kanotra
  • 328
  • 1
  • 3
  • 17