Lyra Orwell
Lyra Orwell

Reputation: 1258

Paramiko SFTPClient.get fails with "FileNotFoundError: [Errno 2] No such file or directory" referring to the remote file path

I am trying to use Paramiko to get a log file from my Raspberry Pi:

import paramiko
paramiko.util.log_to_file("paramiko.log")

# Open a transport
host, port = RECV_IP_ADDRESS, 22
transport = paramiko.Transport((host, port))

# Auth
username, password = "pi", "raspberry"
transport.connect(None, username, password)
# Go!
sftp = paramiko.SFTPClient.from_transport(transport)
# Download
filepath = "/"
localpath = "/home/pi/Code/log.txt"
sftp.get(filepath, localpath)

# Close
if sftp: sftp.close()
if transport: transport.close()

However when I run this program I get a file not found error:

FileNotFoundError: [Errno 2] No such file or directory: '/home/pi/Code/log.txt'

Clearly the file is here

po@raspberrypi:~/Code $ pwd
/home/pi/Code
po@raspberrypi:~/Code $ ls
a.out  log.txt  test.c
po@raspberrypi:~/Code $ _

(terminal screenshot)

But the program can't seem to find it.

I had thought that perhaps the connection had died. I tried a suggestion found here:
Check if paramiko ssh connection is still alive

transport.send_ignore()

Did not yield an error.

I am new to Paramiko. Do I need to run something on the Pi in order for this to work? Is it not simply a wrapper for SFTP/SSH?

Upvotes: 1

Views: 6625

Answers (2)

Martin Prikryl
Martin Prikryl

Reputation: 202282

  1. Your immediate problem is that you have the parameters of SFTPClient.get the other way around (you even have the variables named wrong, the localpath should obviously be remotepath). So Paramiko tries to create a local file with the path of your remote file. As /home/pi/Code does not exist on your local (target) machine, you get local FileNotFoundError error.

  2. Once you correct this, you will have another problem. The localpath path argument of SFTPClient.get needs to be path to the file, not only path to the target directory. See Python Paramiko, PermissionError: [Errno 13] Permission denied when get files from remote server.

So like this:

localpath = "/log.txt"
remotepath = "/home/pi/Code/log.txt"
sftp.get(remotepath, localpath)

Upvotes: 1

Routerdieb
Routerdieb

Reputation: 59

You messed up the order of the parameters. Or use '//' for the file path

Upvotes: 0

Related Questions