import os
import time
from ftplib import FTP
ftp = FTP()
# 打開調試級別2, 顯示詳細信息
# ftp.set_debuglevel(2)
# 服務器IP和端口
ftp.connect("192.168.1.132", 21)
# 匿名登陸, 如果需要登陸, 就把兩個空字符串寫上用戶名和密碼就行了("username", "password")
ftp.login(user="ftpadmin", passwd="123456")
# 切換目錄, 相對於ftp目錄, 比如設置的ftp根目錄為/vat/ftp, 那么pub就是/var/ftp下面的目錄
ftp.cwd("ftpGZDC")
# 查看目錄下有哪些文件, 如果文件名已經存在, 那么再次上傳同一個文件就會報錯, 返回列表
# print(ftp.nlst("/ftpGZDC"))
# 使用二進制的方式打開文件
f_num = 0
bufsize = 1024
start_time = time.time()
for root, dirs, files in os.walk('E:\\test_image\\20210201\\1\\'):
for file in files:
fl = os.path.join(root, file)
file = file.encode("gbk").decode("latin1")
with open(fl, 'rb') as f:
ftp.storbinary('STOR %s' %file, f, bufsize)
f_num = f_num + 1
if int(f_num) % 100 == 0:
print(f_num)
print("共上傳文件:%s 個" %f_num)
# 關閉調試模式
# ftp.set_debuglevel(0)
# 退出FTP連接
ftp.quit()
end_time = time.time()
print('用時:%s 秒' % (end_time - start_time))