import os
from ftplib import FTP
# 创建ftp连接对象
def ftp_connect(host,port, username, password):
ftp = FTP()
# debug级别——2
ftp.set_debuglevel(2)
# 编码格式——utf-8 视情况添加,使用过程中可根据实际情况去掉/添加
ftp.encoding='utf-8'
# host——ftp服务器地址(例:'xxx.xxx.xxx.xxx'),port——端口号
ftp.connect(host, port)
# username——用户名,password密码
ftp.login(username, password)
# 被动模式true开启,false关闭,使用过程中可根据实际情况去掉/添加
#ftp.set_pasv(False)
return ftp
'''
ftp文件上传
local_path——本地地址,source_path——FTP文件夹地址(直接以 /文件夹 开头即可)
注:本地和ftp服务器地址都需精确到具体文件名(例:C:/新建文件夹/新建表格.xls),
没有会自动创建,全部为字符串格式
'''
def upload_file(ftp,local_path,source_path):
if os.path.exists(local_path):
file_writer = open(local_path,'rb')
ftp.storbinary('STOR %s' % source_path,file_writer,8192)
file_writer.close
ftp.quit()
# 下载 此处变量名称同上
def download_file(ftp,local_path,sour
RPA--Python函数分享之——FTP操作
最新推荐文章于 2022-11-04 23:28:48 发布