Sftp.get python paramiko [no such file]

I am writing a Python script that needs to download a remote XML file to parse it.

I use paramiko for this.

Here is the script:

def copyFile(ip, user, pwd, remotePath, localPath):
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
try:
    ssh.connect(ip,username=user,password=pwd)
    ### Copy remote file to server        
    sftp = ssh.open_sftp()
    sftp.get(remotePath,localPath)
    sftp.close()
    ssh.close()
    return ["OK",0,0]
except IOError as e:
    flash(str(e)+" IOERROR")
    return ["IOERROR: " + str(e),0,0]
except Exception as e:
    flash(str(e)+" OTHER EXCEPTION")
    return ["Error: " + str(e),0,0]

The code returns and an IOError says the file does not exist.

But if I change the variable for strings, it works fine .:

   `sftp.get("/etc/conf/file-cfg.xml","./conf/file-cfg.xml")` 

Variables are passed correctly:

    `copyFile(ip,username,pwd,"/etc/conf/"+i,"."+i)`

I lost a lot of time trying to figure out what was wrong with success.

My remote computer: Ubuntu 13.10 x64 Local computer: Windows 7 x64

+4
source share
2 answers

I ran into the same problem, but later found out that there is the following \ n, which is invisible.

strip() / ,

strip() .

+4

Paramiko. , .

localpath = root + '\' + filename
remotepath = root + '\' + filename

, , . SOOOOOOO

local_path = str(localpath)
remote_path = str(remotepath)
sftp.get(remote_path,local_path)

, .

0

Source: https://habr.com/ru/post/1525477/


All Articles