Robert
Robert

Reputation: 37

Pulling a file from SFTP server using partial file name

To obtain a file from a SFTP server I use the command:

sftp.get("directory/filename.ext", preserve_mtime = True)

This works fine when I point to the complete filename. However, there are files on the SFTP server that have a random code in their name together with a date. Can I search for a file in the SFTP directory using only the date part of the filename?

Upvotes: 0

Views: 1854

Answers (1)

Daniel Porteous
Daniel Porteous

Reputation: 6343

You could get a list of all the files in the directory and then check for the date in question:

targetDate = "01-01-2016" # Change to the correct format of course.
possibleFiles = sftp.listdir("directory/")
for i in possibleFiles:
    if targetDate in i:
        sftp.get("directory/" + i, preserve_mtime = True)

You can obviously then do further checks for the file's validity to make sure that it's the one you want.

Upvotes: 3

Related Questions