python 实现ftp上传、下载文件
上传代码:
Host = "23.231.58.204"
UserName = "Zhou"
Pswd = "123456"
import ftplib
# ftp服务直接连接
ftp = ftplib.FTP(Host)
ftp.login(UserName, Pswd)
dir = 'fileName'
try:
# 设置FTP当前操作的路径
ftp.cwd(dir)
except:
# 新建远程目录
ftp.mkd(dir)
ftp.cwd(dir)
command = 'STOR '+ "1212134902.txt"
sendFileName = "E:/project/1233/background" + '/' + "1212134902.txt"
# 上传目标文件
ftp.storbinary(command,open(sendFileName,'rb'))
ftp.quit()
实现效果:

下载代码:
# 下载
import os
import sys
import ftplib
Host = "20.232.58.304"
UserName = "Zhoug"
Pswd = "123456"
ftp = ftplib.FTP(Host)
# 登陆
ftp.login(UserName, Pswd)
localFile = 'E:/project/AOAfile/22222.txt'
remotePath = '/fileName/1212134902.txt'
if(os.path.exists(localFile)):
os.remove(localFile)
# file_handler = open(localFile, 'wb')
open_file = open(localFile,'wb')
ftp.retrbinary('RETR ' + remotePath , open_file.write)
open_file.close()
ftp.close()
本文详细介绍使用Python的ftplib模块进行FTP文件上传和下载的方法。通过具体代码示例,展示了如何连接FTP服务器,上传本地文件到服务器,以及从服务器下载文件到本地。适用于需要在服务器和本地计算机间传输文件的场景。
919

被折叠的 条评论
为什么被折叠?



