通过python下载FTP上的文件夹的实现代码


Posted in Python onFebruary 10, 2013
# -*- 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()
Python 相关文章推荐
用实例解释Python中的继承和多态的概念
Apr 27 Python
举例讲解Python设计模式编程的代理模式与抽象工厂模式
Jan 16 Python
python图片验证码生成代码
Jul 02 Python
Python实现学生成绩管理系统
Apr 05 Python
python使用turtle库绘制时钟
Mar 25 Python
python使用scrapy发送post请求的坑
Sep 04 Python
Python使用Beautiful Soup爬取豆瓣音乐排行榜过程解析
Aug 15 Python
Python学习笔记之集合的概念和简单使用示例
Aug 22 Python
Pytorch之保存读取模型实例
Dec 30 Python
PyQt5 closeEvent关闭事件退出提示框原理解析
Jan 08 Python
Python + selenium + crontab实现每日定时自动打卡功能
Mar 31 Python
python实现跨年表白神器--你值得拥有
Jan 04 Python
打开电脑上的QQ的python代码
Feb 10 #Python
使用python实现baidu hi自动登录的代码
Feb 10 #Python
python判断端口是否打开的实现代码
Feb 10 #Python
python登录QQ邮箱发信的实现代码
Feb 10 #Python
可用于监控 mysql Master Slave 状态的python代码
Feb 10 #Python
python 从远程服务器下载东西的代码
Feb 10 #Python
Python内置的字符串处理函数整理
Jan 29 #Python
You might like
用php来限制每个ip每天浏览页面数量的实现思路
2015/02/24 PHP
WordPress中的shortcode短代码功能使用详解
2016/05/17 PHP
yii2.0整合阿里云oss的示例代码
2017/09/19 PHP
JQuery插件iScroll实现下拉刷新,滚动翻页特效
2014/06/22 Javascript
跟我学习javascript解决异步编程异常方案
2015/11/23 Javascript
Bootstrap富文本组件wysiwyg数据保存到mysql的方法
2016/05/09 Javascript
AngularJS 路由和模板实例及路由地址简化方法(必看)
2016/06/24 Javascript
jQuery ajax请求struts action实现异步刷新
2017/04/19 jQuery
详解Node.js 命令行程序开发教程
2017/06/07 Javascript
基于jquery trigger函数无法触发a标签的两种解决方法
2018/01/06 jQuery
教你搭建按需加载的Vue组件库(小结)
2019/07/29 Javascript
原生js实现点击轮播切换图片
2020/02/11 Javascript
vue-iview动态新增和删除的方法
2020/06/17 Javascript
vue iview 隐藏Table组件里的某一列操作
2020/11/13 Javascript
在Python程序中操作文件之flush()方法的使用教程
2015/05/24 Python
Python面向对象编程基础解析(二)
2017/10/26 Python
Python安装Numpy和matplotlib的方法(推荐)
2017/11/02 Python
基于python实现简单日历
2018/07/28 Python
python 利用for循环 保存多个图像或者文件的实例
2018/11/09 Python
python实现感知器算法(批处理)
2019/01/18 Python
Python爬虫 urllib2的使用方法详解
2019/09/23 Python
python批量处理txt文件的实例代码
2020/01/13 Python
HTML5 Canvas 破碎重组的视频特效的示例代码
2019/09/24 HTML / CSS
美国在线打印网站:Overnight Prints
2018/10/11 全球购物
给护士表扬信
2014/01/19 职场文书
暑期社会实践感言
2014/02/25 职场文书
小学生家长寄语
2014/04/02 职场文书
图书室标语
2014/06/21 职场文书
2014年国庆节广播稿
2014/09/19 职场文书
2014大学辅导员工作总结
2014/12/02 职场文书
2015年八一建军节演讲稿
2015/03/19 职场文书
2015社区六五普法工作总结
2015/04/21 职场文书
Python入门之使用pandas分析excel数据
2021/05/12 Python
swagger如何返回map字段注释
2021/07/03 Java/Android
html网页引入svg图片的4种方式
2022/08/05 HTML / CSS
浅谈css清除浮动(clearfix和clear)的用法
2023/05/21 HTML / CSS