1

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

1
  • 2
    Please don't include screenshots of the error, copy/paste them in code format into your question. Links to images can expire. Commented Jul 27, 2018 at 15:54

1 Answer 1

6

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) 
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.