python ftp编程

python自带有ftp编程的模块ftplib

直接贴几行代码分析

import os, sys

from ftplib import FTP

ftpsite = "ftp.site"
userinfo = ('aaron', getpass('123456'))

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

connection.nlst()  ##nlst() gives files list
                   ##dir() gives full details)
                   
connection.cwd(".")  ##cd to directory

filename = 'filename'
##########download a file#############
localfile = open(filename, 'wb')       ##local file to store download
connection.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()

上面的代码未经测试!

主要是列举几个常见的操作:

登录ftp站点:

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

nlst()  #获取文件列表

retrbinary()  #下载文件

storbinary()  #上传文件

cwd('dir')  #切换到目录

只列举这几个,其它一些操作查看help()


推荐一本python书:python programming(这本书没有讲python基础语法,应用涵盖场景比较广,建议有一点python基础的人看)


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

你可能感兴趣的:(python)