FTP upload files Python

Combined both suggestions. Final answer being

#!/usr/bin/python
import ftplib
import os
filename = "MyFile.py"
ftp = ftplib.FTP("xx.xx.xx.xx")
ftp.login("UID", "PSW")
ftp.cwd("/Unix/Folder/where/I/want/to/put/file")
os.chdir(r"\\windows\folder\which\has\file")
myfile = open(filename, 'r')
ftp.storlines('STOR ' + filename, myfile)
myfile.close()

If you are trying to store a non-binary file (like a text file) try setting it to read mode instead of write mode.

ftp.storlines("STOR " + filename, open(filename, 'rb'))

for a binary file (anything that cannot be opened in a text editor) open your file in read-binary mode

ftp.storbinary("STOR " + filename, open(filename, 'rb'))

also if you plan on using the ftp lib you should probably go through a tutorial, I'd recommend this article from effbot.

Tags:

Python

Ftp