python ftp按date获取最近的文件

我正在使用ftplib连接到一个FTP站点。 我想获得最近上传的文件并下载它。 我能够连接到FTP服务器并列出文件,我也把它们放在一个列表中,并得到了date字段转换。 有什么function/模块可以得到最近的date,并从列表中输出整个行?

#!/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() 

更新:

现在我试图创build两个列表,一个用于date,另一个用于文件名,然后合并它们以形成字典,但是在sorting之后,似乎没有给出正确的值。

有任何想法吗?

  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,value in sorted(who.iteritems(), key =lambda (k,v): (v,k)): print "%s: %s" % (key,value) 

解决:

以前的错误是由于字典按值sorting,我已经更新它按键sorting,它现在的作品,

 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) # print who # for key,value in sorted(who.iteritems(), key =lambda (k,v): (v,k)): 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 

一个问题,是否有可能从字典中检索第一个元素? 我在这里做的是,for循环只运行一次,退出,从而给我的第一个sorting的值这是好的,但我不认为这是一个很好的做法,这样做..

Solutions Collecting From Web of "python ftp按date获取最近的文件"

如果你在time.struct_timestrptime会给你这个)所有的日期列表中,那么你所要做的就是sort列表进行sort

这是一个例子:

 #!/usr/bin/python import time dates = [ "Jan 16 18:35 2012", "Aug 16 21:14 2012", "Dec 05 22:27 2012", "Jan 22 19:42 2012", "Jan 24 00:49 2012", "Dec 15 22:41 2012", "Dec 13 01:41 2012", "Dec 24 01:23 2012", "Jan 21 00:35 2012", "Jan 16 18:35 2012", ] def main(): datelist = [] for date in dates: date = time.strptime(date, '%b %d %H:%M %Y') datelist.append(date) print datelist datelist.sort() print datelist if __name__ == '__main__': main() 

你为什么不使用下一个目录选项?

 ftp.dir('-t',data.append) 

使用此选项,文件列表的时间从最新到最旧。 然后检索列表中的第一个文件来下载它。

我不知道你的ftp是怎么样的,但是你的例子并不适合我。 我改变了与日期排序部分相关的一些行:

  import sys from ftplib import FTP import os import socket import time # Connects to the ftp ftp = FTP(ftpHost) ftp.login(yourUserName,yourPassword) data = [] datelist = [] filelist = [] ftp.dir(data.append) for line in data: col = line.split() datestr = ' '.join(line.split()[5:8]) date = time.strptime(datestr, '%b %d %H:%M') datelist.append(date) filelist.append(col[8]) 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: ftp.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 ftp.quit()