#!/usr/bin/env python#-*-coding:utf-8-*-#@Date: 2018-06-11 09:35:49#@Author: Yaheng Wang ([email protected])#@Link: Http://www.wy2160640.github.io#@Version: $Id $ImportOSImportSYS fromFtplibImportFTPclassFtpsync (object):def __init__(self): Self.conn= FTP ('ftp.hapmap.org') Self.conn.login () self.conn.cwd ('hapmap/frequencies/') Os.chdir ('/home/ftp.hapmap.org/hapmap/frequencies/') defget_dirs_files (self): Dir_res=[] Self.conn.dir ('.', dir_res.append) files= [F.split (None, 8) [-1] forFinchDir_resifF.startswith ('-')] Dirs= [F.split (None, 8) [-1] forFinchDir_resifF.startswith ('D')] returnfiles, dirsdefWalk (Self, Next_dir): Sys.stderr.write ('Walking to%s\n'%next_dir) self.conn.cwd (next_dir)Try: Os.mkdir (next_dir)exceptOSError:PassOs.chdir (next_dir) Ftp_curr_dir=self.conn.pwd () Local_curr_dir=os.getcwd () files, dirs=self.get_dirs_files () sys.stdout.write ("FILES:%s"%files) sys.stdout.write ("DIRS:%s"%dirs) forFinchFiles:sys.stdout.write ("%s:%s"%(Next_dir, F)) Sys.stdout.write ("Download:%s"%Os.path.abspath (f)) Outf= Open (F,"WB") Try: Self.conn.retrbinary ("RETR%s"%F, Outf.write)finally: Outf.close () forDinchDirs:os.chdir (Local_curr_dir) self.conn.cwd (Ftp_curr_dir) self.walk (d)defRun (self): Self.walk ('.')defMain (): F=Ftpsync () f.run ( )if __name__=='__main__': Main ()
Use Python ftplib package to download folders and files recursively