简体   繁体   中英

Uploading a files in a folder to FTP using Python ftplib

I'm trying to upload a bunch of files contained in a single directory.

The code does not fail but does not seems to work either.

My code so far is as follows:

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

Where am I doing wrong?

Thanks in advance.

Ok. I got my code working properly.

Code is:

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

Thanks for the help.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM