通过python下载FTP上的文件夹的实现代码
时间:2022-03-16 11:19:12
# -*- 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,FTP,文件夹
0
投稿
猜你喜欢
jquery ajax 局部无刷新更新数据的实现案例
2024-05-02 17:05:08
python中的迭代和可迭代对象代码示例
2023-02-17 21:13:24
meta标签之详解
2008-01-13 18:48:00
Python迭代用法实例教程
2021-07-18 13:00:45
Python编程产生非均匀随机数的几种方法代码分享
2023-02-10 02:00:19
php实现的微信分享到朋友圈并记录分享次数功能
2023-10-17 04:32:14
基于webpack.config.js 参数详解
2024-05-02 16:28:30
谷歌浏览器Chrome的javascript引擎
2008-09-04 12:24:00
十“问”DreamWeaver
2007-02-03 11:39:00
在EF中使用MySQL的方法及常见问题
2024-01-28 11:22:03
使用 Vue cli 3.0 构建自定义组件库的方法
2024-05-05 09:07:50
Python常用的内置序列结构(列表、元组、字典)学习笔记
2021-02-18 21:25:45
Go如何优雅的使用字节池示例详解
2024-02-10 21:10:17
Python数据结构之双向链表详解
2023-02-01 05:10:07
2008年情人节各网站节日Logo欣赏
2008-02-17 14:12:00
Express框架定制路由实例分析
2024-05-11 10:16:42
Python批量添加图片水印的实现
2021-11-04 17:53:12
vue车牌搜索组件使用方法详解
2024-04-30 10:30:37
分享PHP函数实现数字与文字分页代码
2023-11-14 12:32:54
Pandas Shift函数的基础入门学习笔记
2023-02-16 20:46:12