python编辑ftp客户端

# -*- coding: utf-8 -*-
from ftplib import FTP
import os
import re
from datetime import datetime
S_date = datetime.now().strftime("%Y%m%d")
fnameRegex = re.compile(r'.*%s.*' % S_date)
nameList = []
ftp = FTP()
#ftp.set_debuglevel(2)
ftp.connect('192.168.1.100',21)
ftp.login('user','password')
#ftp.dir()
for line in ftp.nlst():
    nm = fnameRegex.search(line)
    if nm:
        fname = nm.group()
        nameList.append(fname)
    else:
        continue
   
print(nameList)
os.chdir('/backup')
ftp.retrbinary("RETR %s" % nameList[1],open(nameList[1],'wb').write )
#ftp.set_debuglevel(0)
ftp.quit()
posted @ 2021-09-02 15:26  learnnote  阅读(44)  评论(0)    收藏  举报