Nov-30-2017, 11:23 PM
Hi Guys,
I'm sure you will be able to tell from reading this, but I am quite new to python :)
What I'm trying to do is connect to a SFTP server to download files - the files are not always available at the same time each day so I'd like to connect at 2pm and just stay connected polling the directory until all the files are available and then download them.
I have no issues downloading the files if they exist when the script is executed, but because of the delays sometimes they are not there and I'll end up needing to execute it again.
I get run a query using cx_oracle to and put the results into a list - these are all of the filenames that are expected/required to be downloaded for example [file1.csv, file2.csv etc]
The part I'm stuck on / not sure how to do is making the script stay connected to the server until all files are available.
I've got the code below to work on a local folder to tell me when files are added / removed, so I was thinking I might be able to adapt this somehow to just create a list of files that have been added since my connection was made and then I would just somehow compare the list to my expected list that (I mentioned above) somehow?
import os, time path_to_watch = "." before = dict ([(f, None) for f in os.listdir (path_to_watch)]) while 1: time.sleep (10) after = dict ([(f, None) for f in os.listdir (path_to_watch)]) added = [f for f in after if not f in before] removed = [f for f in before if not f in after] if added: print "Added: ", ", ".join (added) if removed: print "Removed: ", ", ".join (removed) before = after
Hoping someone is kind enough to give me some pointers / some sample code that I may be able to adapt...
Many thanks in advance!