Search code examples
pythonsftpparamiko

"No such file" while trying to download file with backslash and space in filename with Paramiko


Trying to create local server. When downloading to client files without whitespaces such as abc.txt, hello-world.png everything works. But there is a problem with downloading files like hello\ world.txt. Program throws

FileNotFoundError: [Errno 2] No such file

Simplified example of my code:

# generating paramiko connection
t = paramiko.Transport((host, port))
t.connect(username=user, password=password)
sftp = paramiko.SFTPClient.from_transport(t)

remote_path = "/home/user/file.txt"
local_path = "/home/localuser/file.txt"
sftp.get(remote_path, local_path) # completing with no errors

remote_path = "/home/user/second\ file.txt"
local_path = "/home/localuser/second\ file.txt"
sftp.get(remote_path, local_path) # FileNotFoundError: [Errno 2] No such file

Solution

  • I assume that the file is actually called second file.txt, not second\ file.txt. You might be confused because in shell one way to escape the space in the file name is using the backslash (though imo more common is to quote the filename). In Paramiko, you do not need to escape the filename (you cannot actually).