我搜索了如何使用 Python 通过 ftp 上传 csv 文件。我尝试了这段代码:
from ftplib import FTP
ftp = FTP("host")
ftp.login("user","password")
Output_Directory = "//ftp//data//"
File2Send="C://Test//test.csv"
file = open(File2Send, "rb")
ftp.cwd(Output_Directory)
ftp.storbinary('STOR ' + File2Send, file)
Run Code Online (Sandbox Code Playgroud)
这就是我得到的错误。我想我无法正确编写 ftp.storbinary 函数。谁能告诉我如何使其正确吗?
谢谢
您不应该使用双正斜杠//。做法是使用双反斜杠,\\因为它们是特殊的转义字符。但正斜杠是正常的。
此外,当使用 ftpSTOR命令时,您已经位于目标目录,因此您必须仅发送所需的文件名,而不是像您正在执行的那样发送完整的本地路径。
Output_Directory = "/ftp/data/"
File2Send="C:/Test/test.csv"
ftp.cwd(Output_Directory)
with open(File2Send, "rb") as f:
ftp.storbinary('STOR ' + os.path.basename(File2Send), f)
Run Code Online (Sandbox Code Playgroud)