【问题标题】:Uploading a files in a folder to FTP using Python ftplib使用 Python ftplib 将文件夹中的文件上传到 FTP
【发布时间】:2021-08-03 19:36:01
【问题描述】:

我正在尝试上传包含在单个目录中的一堆文件。

代码没有失败,但似乎也不起作用。

到目前为止我的代码如下:

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()

我哪里做错了?

提前致谢。

【问题讨论】:

  • 在循环之前添加print(toFtp),或者在调试器下运行并在那里检查..
  • Martin Prikryl:这正是描述,代码没有失败,但也没有完成应有的工作。第二个路径不是远程路径,是要上传的单个文件的本地路径。 PM 77-1:将尝试解决问题。谢谢。
  • 好的。不知道那个。我在这方面绝对是新手,并且没有外部帮助来编码。如果这困扰您,非常欢迎您下次不回答。谢谢。

标签: python ftp upload ftplib


【解决方案1】:

好的。我的代码工作正常。

代码是:

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()

感谢您的帮助。

【讨论】:

    猜你喜欢
    • 2021-12-06
    • 2012-04-21
    • 2020-04-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-21
    相关资源
    最近更新 更多