Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/323.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Python ftplib将文件夹中的文件上载到FTP_Python_Ftp_Upload_Ftplib - Fatal编程技术网

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

使用Python ftplib将文件夹中的文件上载到FTP,python,ftp,upload,ftplib,Python,Ftp,Upload,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&quo

我正试图上传一个目录中包含的一堆文件

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

我的代码如下:

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

感谢您的帮助。

在循环之前添加
print(toFtp)
,或者在调试器下运行并在那里进行检查。