I am using Python to connect to an FTP server that contains a new list of data once every hour. I am only connecting once a day, and I only want to download the newest file in the directory. Is there a way to do this?
2 Answers
Seems like any system that is automatically generating a file once an hour is likely to be using an automated naming scheme. Are you over thinking the problem by asking the server for the newest file instead of more easily parsing the file names?
This wouldn't work in all cases, and if the directory got large it might become time consuming to get the file listing. But it seems likely to work in most cases.

- 12,814
- 10
- 39
- 55
-
I have to do this on more than one server and each customer has a different naming scheme. – scottm Feb 20 '09 at 22:30
-
Well, that sounds like a case for which my suggestion would not work. – acrosman Feb 21 '09 at 02:02
-
actually, have poked around a bit, if the server doesn't respect MDTM, I can't find any reference to other commands that will do the job. You might be stuck with this solution. – acrosman Feb 21 '09 at 02:59
Look at ftplib in your current version of python. You can see a function to handle the result of the LIST command that you would issue to do a dir, if you know a last time that you run a successful script then you can parse the result from the LIST and act on the new files on the directory. See the ftplib for more info on how to do it. The retrlines function is what I would expect to use.

- 10,672
- 3
- 33
- 38