使用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!\n'%local_file)
32                     return False
33                 elif lsize == rsize:
34                     sys.stderr.write('the file %s has been completed!\n'%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)

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM