Python FTP download

Source: Internet
Author: User

I just learned chapter 1 of "Python core programming", FTP programming, and wrote a small script to download all the files on the FTP website. I haven't learned multi-thread programming yet, multi‑thread download will be added later.

from ftplib import FTPimport reimport oshost = 'ftp.neu.edu.cn'dir_path = '/ebook/python'def get_filename(string):    patt = re.compile(r'2011\s(.+)')    filename = re.findall(patt, string)[0]    return filename    def main():    filenames = []    try:        fp = FTP(host)    except Exception:        print "can not connect to ",host        return     print 'connect to ',host,' successfully'    try:        fp.login()    except Exception:        print 'login failed'        return     print 'login successfully'        try:        fp.cwd(dir_path)    except Exception:        print 'change to directory:',dir_path," failed"        fp.quit()        return    print 'change to directory %s successfully' %(fp.pwd())        fp.retrlines('LIST',lambda x:filenames.append(get_filename(x)))    """for filename in filenames:        print filename    return     """    for filename in filenames:        try:            fp.retrbinary('RETR %s' %filename, open(filename,'wb').write)        except Exception ,e:            print e        print '%s download successfully' % filename    fp.quit()    print 'all file download successfully'if __name__ == "__main__":    main()    

Related Article

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.