Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Specify file pattern in pysftp get

We can write a simple get like this:

import pysftp

hostname = "somehost"
user = "bob"       
password = "123456"  
filename = 'somefile.txt'

with pysftp.Connection(hostname, username=user, private_key='/home/private_key_file') as sftp:
    sftp.get(filename)

However, I want to specify a pattern in the filename, something like: '*.txt'

Any idea on how to do this using pysftp ?

like image 583
Saurabh Verma Avatar asked Mar 31 '16 09:03

Saurabh Verma


3 Answers

There's no function to download files matching a file mask in pysftp.

You have to:

  • list the directory, using Connection.listdir or Connection.walktree (if you need recursion)
  • iterate the list of files, filtering the files you want
  • call Connection.get individually for each.

  • For a trivial implementation, see:
    List files on SFTP server matching wildcard in Python using Paramiko

    It's about Paramiko, but the file matching part will be the same with pysftp:

    import fnmatch
    
    for filename in sftp.listdir('/remote/path'):
        if fnmatch.fnmatch(filename, "*.txt"):
            sftp.get("/remote/path/" + filename, "/local/path/" + filename)
    

    Though your should really be using Paramiko anyway: pysftp vs. Paramiko

  • For a recursive example (you have to add the file matching), see:
    Python pysftp get_r from Linux works fine on Linux but not on Windows.

  • See also how Connection.get_d or Connection.get_r are implemented.

like image 99
Martin Prikryl Avatar answered Oct 19 '22 08:10

Martin Prikryl


Can confirm after going through the documentation that you can't list using a pattern. So i did something like this:

import pysftp
import re

server = pysftp.Connection(host=FTP_HOST,
                  username=FTP_USERNAME,
                  password=FTP_PASSWORD)
server.cwd(YOUR_FILES_PATH)
filelist = server.listdir()

for filename in filelist:
    filedate = re.search(".*\.txt$", filename)
    if filedate:
        print "FOUND FILE " + filename
like image 7
Richard Avatar answered Oct 19 '22 07:10

Richard


import pysftp
import sys

[...]

dn = datetime.now().strftime("%Y%m%d%H");
with pysftp.Connection(myHost, myUsername, password=myPassword) as sftp:
    myFileList = sftp.listdir("files/")
    for filename in myFileList:
        if (filename.rfind("ArrivalList_" + dn) != -1):
            sftp.get("files/" + filename, "/tmp/" + filename)
like image 1
Bob Yoplait Avatar answered Oct 19 '22 09:10

Bob Yoplait