这是列出FTP服务器的所有子目录的代码。如何搜索位于这些多个子目录目录中的最新Excel文件?如结果所示,我想浏览所有ls****
子目录,如果有一个包含今天日期的Excel文件,请通知我。
提前致谢!
from ftplib import FTP
def handleDownload(block):
file.write(block)
print ".",
ftp = FTP('connect to host,'name', 'dflt port')
directory = 'Dir_name'
ftp.cwd(directory)
data = []
ftp.dir(data.append)
ftpContentList = ftp.retrlines('LIST') # list directory contents
结果:
drwxrwx--- 4 17610000 smartfile 4096 Jul 21 19:31 ls0125
drwxrwx--- 4 17610000 smartfile 4096 Jul 19 20:34 ls0146
drwxrwx--- 4 17610000 smartfile 4096 Jul 21 19:31 ls0265
drwxrwx--- 4 17610000 smartfile 4096 Jul 19 20:34 ls0368
答案 0 :(得分:1)
您将要在ftp.retlines中注册一个回调函数,如此
def callback(line):
try:
#only use this code if you'll be dealing with that FTP server alone
#look into dateutil module which parses dates with more flexibility
when = datetime.strptime(re.search('[A-z]{3}\s+\d{1,2}\s\d{1,2}:\d{2}', line).group(0), "%b %d %H:%M")
today = datetime.today()
if when.day == today.day and when.month == today.month:
pass #perform your magic here
except:
print "failed to parse"
return
ftp.retrlines('LIST', callback)