tags:

views:

187

answers:

4

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?

+1  A: 

This should get you headed in the right direction.

Chris Ballance
This FTP Server apparently doesn't understand the MTDM command.
scottm
...or the MDTM command :)
scottm
@scotty2012: well that answers my question about whether you have any control at all over server side :)
Van Gale
A: 

Check out the ftpmirror.py script

http://www.koders.com/python/fidA4B9A5675F5B9101CEAB1A1E7FF8A17E73D22149.aspx?s=cdef%3Aparser

A: 

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.

Andrew Cox
+1  A: 

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.

acrosman
I have to do this on more than one server and each customer has a different naming scheme.
scottm
Well, that sounds like a case for which my suggestion would not work.
acrosman
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