I am using Python Paramiko to retrieve/search file from an SFTP server. I get all file in the directory.
What I need is specific file from that directory. How do I get it?
I am using Python Paramiko to retrieve/search file from an SFTP server. I get all file in the directory.
What I need is specific file from that directory. How do I get it?
Use Paramiko SFTPClient.get
to download a single file:
with paramiko.SSHClient() as ssh:
ssh.connect(host, username=username, password=password)
with ssh.open_sftp() as sftp:
sftp.get("/remote/path/file.txt", "/local/path/file.txt")
You will also have to deal with the server s host key verification.
What you need to do is create an ssh client, then execute an ls
with piped grep
to find your file. such as ls /srv/ftp | grep ^FTP_
to find files in the /srv/ftp
directory and start with FTP
. Then open an sftp connection and execute the get
command to bring the files over.
EDIT: Martin below mentioned that there is a better way to get the directory contents using SFTPClient.listdir() - I have revised to that method. More info in the docs: https://docs.paramiko.org/en/stable/api/sftp.html
Putting all that together looks like
import paramiko
host =
port = 22
username =
password =
with paramiko.SSHClient() as client:
client.connect(host, port, username, password)
with client.open_sftp() as sftp:
files = sftp.listdir( /srv/ftp )
for i, file in enumerate(files):
if file and file.startswith( FTP ):
sftp.get(f /srv/ftp/{file} , f ~/ftp/{file} )
print(f Moved {file} )
This code is untested but should work. Hope that s clear.
Here is an answer in case you need a kind of find
using a SFTP connection, not knowing the exact path and name of the file. If it is not what you were looking for, I am sorry.
I made a library named sftputil, based on paramiko, which implements advanced functionalities such as glob
. To find a specific file and download it, you can do it this way :
from sftputil import SFTP
sftp = SFTP("hostname", "username", password="password")
# Here we look for a file with a name starting with `foo`
found_files = sftp.glob("directory/foo*")
# Here we look for the file `bar` in any subdirectory
found_files = sftp.glob("directory/*/bar")
# But you can use other patterns of course.
# And now the files can be downloaded
for f in found_files:
sftp.get(f, "my/local/path")
If you don’t know glob
, you should read the python documentation, as this implementation works the same way.
Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...
I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...
Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...
Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...
I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...
Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...
Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...
I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...