2016-07-04 7 views
0

イム私のFTPサーバ内のファイルは
PythonのFTP filematchにのみ最後のファイルベースの取得

testing_01.xml
testing_02.xml
testing_03.xml(これですある最新のファイル)

from ftplib import FTP 
ftp = FTP('FTP IP') # open FTP connection to MSM 
ftp.login(user = 'username',passwd = 'password') 
ftp.dir() 
directory ="/temp/" #directory where file should downloaded 
filematch = 'testing_*.xml' # a file match to be downloaded 
ftp.cwd(directory) 
for filename in ftp.nlst(filematch): # Loop - looking for matching files 
    fhandle = open(filename, 'wb') 
    ftp.retrbinary('RETR ' + filename, fhandle.write) 
    fhandle.close() 

スクリプト上記のftpからすべてのファイルを取得します

私の目的は最新のファイル "testing_03.xml"を取得することです。

答えて

1

ftp.nlst()はファイル名のリストを返します。だから['testing_01.xml', 'testing_02.xml', 'testing_03.xml']となるでしょう。 リストの最後の項目はインデックス-1です。常に、最後のファイルを取得する手順は次のとおりです。[?どのような答えの仕事を受け入れない]

file_list = ftp.nlst(filematch) 
file_i_want = file_list[-1] 

か、単に

filename = ftp.nlst(filematch)[-1] 
fhandle = ... # the rest of what you have in your for loop, but not as a loop 
+0

それが今 – user3584454

+0

作業だが、どうもありがとうございました(のhttp://メタ.stackexchange.com/a/5235/193893)&[誰かが私の質問に答えるとどうすればいいですか?](http://stackoverflow.com/help/someone-answers) – aneroid

関連する問題