Python operates on FTP

Source: Internet
Author: User
Tags ftp connection ftp hostname

Background: FTP operation is required, involving upload and download operations 
 fromFtplibImportFTP FTP= FTP (host= "ftp hostname", user= ' login username ', passwd= ' login password ') # Connect to FTPFtp.set_debuglevel (2)#turn on debug Level 2, show more information defDownload (filename,bufsize=1024):#bufsize The buffer size setFile_handle = open (filename,"WB"). Write#to open a file locally in write modeFtp.retrbinary ("RETR%s"%filename, File_handle, bufsize)#receiving files on the server and writing to local files defUpload (filename,bufsize=1024): File_handle2= open (filename,"RB")#to open a local file in read-only modeFtp.storbinary ('STOR%s'%filename, File_handle2, bufsize)#upload local filename to serverftp.getwelcome ()#Print out a welcome messageFTP.CWD ('Directory name')#go to remote directoryFTP.RMD (' Directory name'# Delete the directory on FTPFTP.MKD ('Directory name')#Create a directory on FTPFtp.pwd ()#Print the current FTP directoryFtp.dir ()#Print all files in the current directory on FTPftp.quit ()#Exit FTP

Specific Description:1.FTP = FTP (host= "ftp hostname",user= ' login username ', passwd=' login password ') FTP () is actually called. Login to log in 2.download downloading files from FTPlocal files need to be opened in WB write mode and then downloaded via the Retrinary () method 3.upload uploading local files to FTP serverThe local file is opened in read-only mode and then uploaded via the Storbinary () method 4. Some other common methodsgetwelcome (): Print the FTP welcome, set on the FTP servercwd (' Directory name '): Enter a directory on the FTP server and switch directoriesRMD (' Directory name '): Delete directoryMKD (' Directory name '): Create directoryftp.pwd (): Print the current directorydir (): Print all files in the current directoryquit () or close (): Close FTP connection Note: If you want to implement the upload directory or upload files according to regular match, you need to write the method separately

Python operates on FTP

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.