zoukankan      html  css  js  c++  java
  • 使用Python批量下载ftp服务器中的内容

    使用ftplib,轻松实现从ftp服务器上下载所需要的文件,包括目录结构等,支持了一下断点续传

     1 from ftplib import FTP
     2 import sys
     3 import os
     4 import re
     5 
     6 def ftpconnet(ftpserver,port,username,password):
     7     ftp = FTP()
     8     try:
     9         ftp.connect(ftpserver,port)
    10     except:
    11         raise IOError,'FTP connect failed!'
    12 
    13     try:
    14         ftp.login(username,password)
    15     except:
    16         raise IOError,'FTP login failed!'
    17     else:
    18         return ftp
    19 
    20 def ftpdownload(ftp,ori_path,dest_path):
    21     for each in ftp.nlst(ori_path):
    22         try:
    23             ftp.cwd(each)
    24         except:
    25             filename = re.search('S+/(S+)',each).group(1)
    26             local_file = dest_path + '/' + filename
    27             if os.path.exists(local_file):
    28                 lsize = os.stat(local_file).st_size
    29                 rsize = ftp.size(each)
    30                 if lsize > rsize:
    31                     sys.stderr.write('the local file %s is bigger than the remote!
    '%local_file)
    32                     return False
    33                 elif lsize == rsize:
    34                     sys.stderr.write('the file %s has been completed!
    '%local_file)
    35                 bufsize = 1024 * 1024
    36                 fp = open(local_file,'ab')
    37                 ftp.retrbinary('RETR '+each,fp.write,bufsize,rest=lsize)
    38             else:
    39                 bufsize = 1024 * 1024
    40                 fp = open(local_file,'wb')
    41                 ftp.retrbinary('RETR '+each,fp.write,bufsize)
    42         else:
    43             dirname = re.search('S+/(S+)',each).group(1)
    44             dirname = dest_path + '/' + dirname + '/'
    45             os.system('mkdir -p %s'%dirname)
    46             ftpdownload(ftp,each,dirname)
    47 
    48 def ftpclose(ftp):
    49     ftp.quit()
    50 
    51 if __name__ == '__main__':
    52     ftp = ftpconnet('climb.genomics.cn',21,'','')
    53     ftpdownload(ftp,'/pub/10.5524/100001_101000/100145/','./')
    54     ftpclose(ftp)
  • 相关阅读:
    android开发 软键盘出现后 防止EditText控件遮挡 总体平移UI
    jQuery中this与$(this)的差别
    纯手写wcf代码,wcf入门,wcf基础教程
    JavaScript权威指南第01章 JavaScript 概述
    Python
    微信支付界面中文乱码问题
    EasyUI基础入门之Pagination(分页)
    Maximum Subarray
    CF1063F String Journey
    排序
  • 原文地址:https://www.cnblogs.com/lyon2014/p/4565976.html
Copyright © 2011-2022 走看看