I am using ftplib to connect to an ftp site. I want to get the most recently uploaded file and download it. I am able to connect to the ftp server and list the files, I also have put them in a list and got the datefield
converted. Is there any function/module which can get the recent date and output the whole line from the list?
#!/usr/bin/env python
import ftplib
import os
import socket
import sys
HOST = 'test'
def main():
try:
f = ftplib.FTP(HOST)
except (socket.error, socket.gaierror), e:
print 'cannot reach to %s' % HOST
return
print "Connect to ftp server"
try:
f.login('anonymous','al@ge.com')
except ftplib.error_perm:
print 'cannot login anonymously'
f.quit()
return
print "logged on to the ftp server"
data = []
f.dir(data.append)
for line in data:
datestr = ' '.join(line.split()[0:2])
orig-date = time.strptime(datestr, '%d-%m-%y %H:%M%p')
f.quit()
return
if __name__ == '__main__':
main()
RESOLVED:
data = []
f.dir(data.append)
datelist = []
filelist = []
for line in data:
col = line.split()
datestr = ' '.join(line.split()[0:2])
date = time.strptime(datestr, '%m-%d-%y %H:%M%p')
datelist.append(date)
filelist.append(col[3])
combo = zip(datelist,filelist)
who = dict(combo)
for key in sorted(who.iterkeys(), reverse=True):
print "%s: %s" % (key,who[key])
filename = who[key]
print "file to download is %s" % filename
try:
f.retrbinary('RETR %s' % filename, open(filename, 'wb').write)
except ftplib.err_perm:
print "Error: cannot read file %s" % filename
os.unlink(filename)
else:
print "***Downloaded*** %s " % filename
return
f.quit()
return
One problem, is it possible to retrieve the first element from the dictionary? what I did here is that the for loop runs only once and exits thereby giving me the first sorted value which is fine, but I don't think it is a good practice to do it in this way..
If you have all the dates in
time.struct_time
(strptime
will give you this) in a list then all you have to do issort
the list.Here's an example :
I don't know how it's your ftp, but your example was not working for me. I changed some lines related to the date sorting part:
For those looking for a full solution for finding the latest file in a folder:
MLSD
If your FTP server supports
MLSD
command, a solution is easy:LIST
If you need to rely on obsolete
LIST
command, you have to parse a proprietary listing it returns.Common *nix listing is like:
With a listing like this, this code will do:
This is a rather fragile approach.
MDTM
A more reliable, but a way less efficient, is to use
MDTM
command to retrieve timestamps of individual files/folders:Non-standard -t switch
Some FTP servers support a proprietary non-standard
-t
switch forNLST
(orLIST
) command.See How to get files in FTP folder sorted by modification time.
Downloading found file
No matter what approach you use, once you have the
latest_name
, you download it as any other file:See also
Why don't you use next dir option?
With this option the file listing is time ordered from newest to oldest. Then just retrieve the first file in the list to download it.