python - 使用 Python ftplib 将文件夹中的文件上传到 FTP
问题描述
我正在尝试上传包含在单个目录中的一堆文件。
该代码没有失败,但似乎也不起作用。
到目前为止,我的代码如下:
import ftplib
FTP_HOST = "host"
FTP_USER = "user"
FTP_PASS = "pass"
ftp = ftplib.FTP(FTP_HOST, FTP_USER, FTP_PASS)
ftp.encoding = "utf-8"
dirFTP = "dirPath"
toFTP = os.listdir(dirFTP)
for filename in toFTP:
filePath = os.path.join(dirFTP, filename)
with open(filePath, "rb") as file:
ftp.storbinary(f"STOR {filePath}", file)
ftp.quit()
我在哪里做错了?
提前致谢。
解决方案
好的。我的代码工作正常。
代码是:
import ftplib
FTP_HOST = "host"
FTP_USER = "user"
FTP_PASS = "pass"
ftp = ftplib.FTP(FTP_HOST, FTP_USER, FTP_PASS)
ftp.encoding = "utf-8"
dirFTP = "dirPath"
toFTP = os.listdir(dirFTP)
for filename in toFTP:
with open(os.path.join(dirFTP,filename), 'rb') as file: #Here I open the file using it's full path
ftp.storbinary(f'STOR {filename}', file) #Here I store the file in the FTP using only it's name as I intended
ftp.quit()
谢谢您的帮助。
推荐阅读
- php - php中mysql的两个日期之间的总和
- javascript - 序列化表单并使用标签和段落作为数组中的键
- laravel-5.7 - Laravel db:在控制器上选择
- c - 指针和结构变量
- sql-server - 如何有条件地从不同的表中插入 INSERT INTO?
- sockets - 尝试使用 ws-rs (mio) 连接到 https websocket 时出现“资源暂时不可用”
- psychopy - PsychoPy 使用 KeyList 设置实验
- webpack - Webpack - Babel 错误 - 找不到相对于目录的预设“@babel/env”
- php - 如何添加我自己的操作以在用户下新订单并删除电子邮件操作时触发
- c# - 实例化的预制件不移动