国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

通過python下載FTP上的文件夾的實現代碼

2019-11-25 18:41:22
字體:
來源:轉載
供稿:網友
復制代碼 代碼如下:

# -*- encoding: utf8 -*-
import os
import sys
import ftplib
class FTPSync(object):
    def __init__(self):
        self.conn = ftplib.FTP('10.22.33.46', 'user', 'pass')
        self.conn.cwd('/')        # 遠端FTP目錄
        os.chdir('/data/')        # 本地下載目錄
    def get_dirs_files(self):
        u''' 得到當前目錄和文件, 放入dir_res列表 '''
        dir_res = []
        self.conn.dir('.', dir_res.append)
        files = [f.split(None, 8)[-1] for f in dir_res if f.startswith('-')]
        dirs = [f.split(None, 8)[-1] for f in dir_res if f.startswith('d')]
        return (files, dirs)
    def walk(self, next_dir):
        print 'Walking to', next_dir
        self.conn.cwd(next_dir)
        try:
            os.mkdir(next_dir)
        except OSError:
            pass
        os.chdir(next_dir)
        ftp_curr_dir = self.conn.pwd()
        local_curr_dir = os.getcwd()
        files, dirs = self.get_dirs_files()
        print "FILES: ", files
        print "DIRS: ", dirs
        for f in files:
            print next_dir, ':', f
            outf = open(f, 'wb')
            try:
                self.conn.retrbinary('RETR %s' % f, outf.write)
            finally:
                outf.close()
        for d in dirs:
            os.chdir(local_curr_dir)
            self.conn.cwd(ftp_curr_dir)
            self.walk(d)
    def run(self):
        self.walk('.')
def main():
    f = FTPSync()
    f.run()
if __name__ == '__main__':
    main()
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 富顺县| 辛集市| 宁南县| 博罗县| 荣昌县| 清新县| 长治市| 巴青县| 延长县| 青铜峡市| 杭州市| 古田县| 蒙城县| 延寿县| 安吉县| 房产| 石河子市| 同德县| 大悟县| 军事| 南安市| 久治县| 屯门区| 富源县| 永平县| 灵台县| 鄂伦春自治旗| 虞城县| 桓仁| 天镇县| 城市| 南阳市| 望江县| 镇江市| 美姑县| 稷山县| 隆回县| 恩平市| 旺苍县| 巴南区| 嘉荫县|