1

これは、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
4

1 に答える 1

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)
于 2010-09-15T17:00:29.043 に答える