# -*- coding:utf-8 -*-
# date:2021-10-20
from ftplib import FTP
import os
def ftp_connect(host, username, password, port=21):
ftp = FTP()
ftp.encoding='gbk'
ftp.connect(host, port, timeout=60)
ftp.login(username, password)
return ftp
# def file_list(my_ftp,remote_path):
# my_ftp.cwd(remote_path)
# file_list = my_ftp.nlst()
# return file_list
def download_files(local_path, remote_path):
my_ftp.cwd(remote_path)
file_list = my_ftp.nlst()
for filename in file_list:
print(filename)
full_filename = os.path.join(local_path, filename)
buffer_size = 1024
f = open(full_filename, 'wb')
my_ftp.retrbinary("RETR %s" % filename, f.write, buffer_size)
f.close()
def upload_files(local_path,remote_path):
my_ftp.cwd(remote_path)
file_list = os.listdir(local_path)
for local_filename in file_list:
local_fullfile = os.path.join(local_path, local_filename)
buff_size = 1024
file_handler = open(local_fullfile, 'rb')
my_ftp.storbinary("STOR %s" % local_filename, file_handler, buff_size)
file_handler.close()
def myftp_close():
my_ftp.quit()
if __name__ == '__main__':
my_ftp = ftp_connect('127.0.0.1', 'ftp1', '1234567')
local_path = r'F:/temp'
remote_path = r"ftp1/"
# download_files(local_path, remote_path)
upload_files(local_path,remote_path)
myftp_close()
2021-10-24python实现ftp 上传下载文件功能
最新推荐文章于 2023-07-25 11:45:04 发布