微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

使用 Python ftplib 将文件夹中的文件上传到 FTP

如何解决使用 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()

感谢您的帮助。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。