tags:

views:

23

answers:

2

This is the code that lists all the subdirectories from FTP server. How do I search for the most recent Excel files sitting in these multiple Subdirectories directory? As shown in the results, I want to go through all the ls**** subdirectories and notify me if there is an Excel file with today's date.

Thanks in advance!

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

Results:

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
+1  A: 

You're going to want to register a callback function in ftp.retlines like so

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)
Novikov
Thanks for the reply. I am new to programming, can you please elaborate more into Parsing the line and extracting date? thanks
Guru1
I updated the example, but note that that is a very dirty implementation and you should just learn from it how not to do it
Novikov
A: 

Thanks,

I modified the program and when I ran it I am getting "failed to parse". Please help!

Guru1