簡體   English   中英

使用 Python ftplib 將文件夾中的文件上傳到 FTP

[英]Uploading a files in a folder to FTP using Python ftplib

我正在嘗試上傳包含在單個目錄中的一堆文件。

該代碼沒有失敗,但似乎也不起作用。

到目前為止,我的代碼如下:

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

謝謝您的幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM