2.7 清除FTP服务器文件
清除服务器文件
from ftptools import FtpTools class CleanAll(FtpTools): '''delete an entire remote tree of subdirectories''' def __init__(self): self.fcount = self.dcount = 0 def getlocaldir(self): return None def getcleanall(self): return True def cleanDir(self): lines = [] self.connection.dir(lines.append) #每个文件Lines都不同 for line in lines: parsed = line.split() permiss = parsed[0] #判断drw... 文件名 fname = parsed[-1] if fname in ('.','..'):continue elif permiss[0] != 'd': print('file',fname) self.connection.delete(fname) self.fcount += 1 else: print('directory',fname) self.connection.cwd(fname) self.cleanDir() self.connection.cwd('..') self.connection.rmd(fname) self.dcount += 1 print('directory exited') if __name__ == '__main__': ftp = CleanAll() ftp.configTransfer(site='192.168.191.1', rdir='.',user='') ftp.run(cleanTarget=ftp.cleanDir) print('Done:', ftp.fcount, 'files and', ftp.dcount, 'directory cleaned.')