Python: 正在上传文件FTP_TLS- "550 参数不正确"

Python: Uploading files FTP_TLS- "550 The parameter is incorrect"

我正在尝试使用 TLS 连接到 FTP 服务器并上传文本文件。下面的代码连接到网站就好了,但它没有上传文件。相反,我收到以下错误:

Traceback (most recent call last):
  File "X:/HR & IT/Ryan/Python Scripts/ftps_connection_test.py", line 16, in <module>
    ftps.storlines("STOR " + filename, open(filename,"r"))
  File "C:\Python33\lib\ftplib.py", line 816, in storlines
    with self.transfercmd(cmd) as conn:
  File "C:\Python33\lib\ftplib.py", line 391, in transfercmd
    return self.ntransfercmd(cmd, rest)[0]
  File "C:\Python33\lib\ftplib.py", line 756, in ntransfercmd
    conn, size = FTP.ntransfercmd(self, cmd, rest)
  File "C:\Python33\lib\ftplib.py", line 357, in ntransfercmd
    resp = self.sendcmd(cmd)
  File "C:\Python33\lib\ftplib.py", line 264, in sendcmd
    return self.getresp()
  File "C:\Python33\lib\ftplib.py", line 238, in getresp
    raise error_perm(resp)
ftplib.error_perm: 550 The parameter is incorrect. 

我可能缺少一些非常基本的东西,我的代码在下面,非常感谢任何帮助。

import os
from ftplib import FTP_TLS as f

# Open secure connection
ftps = f("ftp.foo.com")
ftps.login(username,password)  
ftps.prot_p()                        

# Create the test txt file to upload 
filename = r"c:\path\to\file"
testFile = open(filename,"w")
testFile.write("Test file with test text")
testFile.close()

# Transfer testFile
ftps.storlines("STOR " + filename, open(filename,"r"))

# Quit connection
ftps.quit()
filename = r"c:\path\to\file"

是本地文件的绝对路径。在 STOR 命令中传递了相同的值,即

ftps.storlines("STOR " + filename, open(filename,"r"))

尝试执行 STOR c:\path\to\file 操作,但是,该路径不太可能存在于远程服务器上,ftplib.error_perm 异常表明您没有 允许在那里写(即使它确实存在)。

您可以试试这个:

ftps.storlines("STOR " + os.path.basename(filename), open(filename,"r"))

这将发出 STOR file 操作并将文件上传到远程服务器上的默认目录。如果您需要上传到远程服务器上的不同路径,只需将其添加到 STOR。

我在尝试将文件上传到 FTP 服务器时遇到了同样的错误。就我而言,目标文件名的格式不正确。有点像 data_20180411T12:00:12.3435Z.txt

我重命名为 data_20180411T120012_3435Z.txt。然后就成功了。