啊峰哥

2.6 利用FTP上传所有文件

利用FTP上传所有文件

import os,ftptools

class UploadAll(ftptools.FtpTools):                              #继承上一篇写的Ftptools
    '''upload an entire tree of subdirectories'''

    def __init__(self):
        self.fcount = self.dcount = 0

    def getcleanall(self):
        return False

    def uploadDir(self,localdir):
        '''for each directory in an entire tree,upload simple files,recur into subdirectories'''

        localfiles = os.listdir(localdir)
        for localname in localfiles:
            localpath = os.path.join(localdir,localname)
            print('uploading',localpath,'to',localname,end=' ')
            if not os.path.isdir(localpath):
                self.uploadOne(localname,localpath,localname)    #使用Ftptools中的上传方法
                self.fcount += 1
            else:
                try:
                    self.connection.mkd(localname)               #创建目录
                    print('directory created')
                except:
                    print('directory existed')
                #开始上传目录
                    self.connection.cwd(localname)
                    self.uploadDir(localpath)
                    self.connection.cwd('..')
                    self.dcount += 1
                    print('directory uploaded')

if __name__ == '__main__':
    ftp = UploadAll()
    ftp.configTransfer(site='192.168.191.1',rdir='.',user=())
    ftp.run(transferAct=lambda :ftp.uploadDir(ftp.localdir))
    print('Done:',ftp.fcount,'files and',ftp.dcount,'directory uploaded.')

 

posted on 2017-10-14 12:29  啊峰哥  阅读(499)  评论(0编辑  收藏  举报

导航