tags:

views:

890

answers:

4

How do I determine the most recently modified file from an ftp directory listing? I used the max function on the unix timestamp locally, but the ftp listing is harder to parse. The contents of each line is only separated by a space.

from ftplib import FTP
ftp = FTP('ftp.cwi.nl')
ftp.login()
data = []
ftp.dir(data.append)
ftp.quit()
for line in data:
    print line

output:

drwxrwsr-x   5 ftp-usr  pdmaint     1536 Mar 20 09:48 .
dr-xr-srwt 105 ftp-usr  pdmaint     1536 Mar 21 14:32 ..
-rw-r--r--   1 ftp-usr  pdmaint     5305 Mar 20 09:48 INDEX
A: 

You can split each line and get the date:

date_str = ' '.join(line.split(' ')[5:8])

Then parse the date (check out egenix mxDateTime package, specifically the DateTimeFromString function) to get comparable objects.

scrible
+1  A: 

To parse the date, you can use (from version 2.5 onwards):

datetime.datetime.strptime('Mar 21 14:32', '%b %d %H:%M')
Nimrod
+1  A: 

Just to make some corrections:

date_str = ' '.join(line.split()[5:8])
time.strptime(date_str, '%b %d %H:%M') # import time

And to find the most recent file

for line in data:
    col_list = line.split()
    date_str = ' '.join(line.split()[5:8])
    if datePattern.search(col_list[8]):
        file_dict[time.strptime(date_str, '%b %d %H:%M')] = col_list[8]
        date_list = list([key for key, value in file_dict.items()])
s = file_dict[max(date_list)]
print s
yuguang
+1  A: 

If the FTP server supports the MLSD command (and quite possibly it does), you can use the FTPDirectory class from that answer in a related question.

Create an ftplib.FTP instance (eg aftp) and an FTPDirectory instance (eg aftpdir), connect to the server, .cwd to the directory you want, and read the files using aftpdir.getdata(aftp). After that, you get name of the freshest file as:

import operator

max(aftpdir, key=operator.attrgetter('mtime')).name
ΤΖΩΤΖΙΟΥ