Python implementation ftp file download

Source: Internet
Author: User
Tags ftp file

Python version 2.7


Code:

#-*-coding:utf-8-*-Import OS import time from ftplib import FTP #服务器地址 ftp_server= ' xx.xx.xx.xx '--corresponding FTPE server address USER = ' user ' PWD = ' password ' ftp_path= '/home/xx/xx/' local_root= ' e: ' +ftp_path date= (' Time.strftime ',%y%m%d (
        Time.time ()) Print DATE def isdir (filename): Try:path = filename; Path.replace ('/', ' \ \ ') if Os.path.exists (path): print '---file exists--' else:pri NT ' File not exists ', Local_root os.mkdirs (local_root) return True Except:return
    False def ftpconnect (): Ftp=ftp () ftp.set_debuglevel (2) ftp.connect (ftp_server,21) ftp.login (USER,PWD) return ftp def downloadfile (): FTP = Ftpconnect () print ftp.getwelcome () #显示ftp服务器欢迎信息 li = ftp.nl St (ftp_path) print ' FTP: ', Li for eachfile in li:localpath = ' e: ' +eachfile print '--open Localpa
     Th--', localpath bufsize = 1024 Isdir (localpath)   fp = open (LocalPath, ' wb+ ') ftp.retrbinary (' RETR ' +eachfile,fp.write,bufsize) Fp.flush () ftp.se T_debuglevel (0) #关闭调试 fp.close () ftp.quit () #退出ftp服务器 if __name__== "__main__": DownloadFile ()


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.