Download the implementation code for the folder on FTP via Python _python

Source: Internet
Author: User
Copy Code code as follows:

#-*-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 ('/') # Remote FTP directory
Os.chdir ('/data/') # Local download directory
def get_dirs_files (self):
U ' get current directory and file, put into Dir_res list '
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 ()

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.