Cannot copy/move file from remote SFTP server to local machine by Paramiko code running on remote SSH server

Question:

I want to copy a file from my SFTP server to local computer. However, when I run my code, it didn’t show any error while I still cannot find my file on local computer. My code like that:

import paramiko
host_name ='10.110.100.8'
user_name = 'abc'
password ='xyz'
port = 22
remote_dir_name ='/data/.../PMC1087887_00003.jpg' 
local_dir_name = 'D:..pred.jpg'

t = paramiko.Transport((host_name, port))
t.connect(username=user_name, password=password)
sftp = paramiko.SFTPClient.from_transport(t)
sftp.get(remote_dir_name,local_dir_name)

I have found the main problem. If I run my code in local in VS Code, it works. But when I login in my server by SSH in VS Code, and run my code on server, I found that my file appeared in current code folder (for example /home/.../D:..pred.jpg) and its name is D:..pred.jpg. How to solve this problem if I want to run code on server and download file to local?

Asked By: 4daJKong

||

Answers:

If you call SFTPClient.get on the server, it will, as any other file manipulation API, work with files on the server.

There’s no way to make remote Python script directly work with files on your local machine.

You would have to use some API to push the files to your local machine. But for that, your local machine would have to implement the API. For example, you can run an SFTP server on the local machine and "upload" the files to it.

Answered By: Martin Prikryl
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.