Python FTP Programming

Source: Internet
Author: User

Python self-developed FTP programming module ftplib

Directly paste several lines of code for analysis

import os, sysfrom ftplib import FTPftpsite = "ftp.site"userinfo = ('aaron', getpass('123456'))print('Connecting')connection = ftplib.FTP(sitename)connnection.login(*userinfo)  ##() for anonymous loginconnection.nlst()  ##nlst() gives files list                   ##dir() gives full details)                   connection.cwd(".")  ##cd to directoryfilename = 'filename'##########download a file#############localfile = open(filename, 'wb')       ##local file to store downloadconnection.retrbinary('RETR ' + filename, localfile.write, 1024)localfile.close()##########download a file#######################upload a file#############localfiletoupload = open(filename, 'rb')connection.storbinary('STOR ' + filename, localfiletoupload, 1024)##########upload a file#############connection.quit()

The above Code has not been tested!

It mainly lists several common operations:

Log on to the FTP site:

connection = ftplib.FTP(sitename)connnection.login(*userinfo)  ##() for anonymous login

NLST () # obtain the file list

Retrbinary () # download an object

Storbinary () # upload a file

CWD ('dir') # Switch to the directory

Only list these items. For other operations, see help ()

I recommend a python book: Python programming (this book does not talk about the basic Python syntax and has a wide range of application scenarios. It is recommended that you have a bit of Python basics)

connection = ftplib.FTP(sitename)connnection.login(*userinfo)  ##() for anonymous login

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.