Reputation: 31
I've searched how to upload a csv file by ftp using Python. I tried this code:
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)
and that's what I got as an error. I think I couldn't write ftp.storbinary function correctly. Can anyone tell me how to make it correct please?
Thank you
Upvotes: 1
Views: 7671
Reputation: 222852
You shouldn't double forward slashes //
. The practice is to double backslashes \\
because they are special escape characters. But forward slashes are normal.
Also when using the ftp STOR
command you are already at the destination directory so you must send only the filename you want, not the full local path like you're doing.
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)
Upvotes: 6