Programming

Python 3 – upload files over FTP

steloflute 2015. 8. 19. 23:30

source: http://daanlenaerts.com/blog/2015/08/18/python-3-upload-files-over-ftp/

 

FTP is a very popular, and commonly used file transfer protocol. Almost everyone who has a website, has used FTP to put it online. It’s easy to use, fast, and – when using SFTP – secure as well. It can be really useful to be able to transfer files over FTP with a Python script. So that’s what I’ll be doing right now.

First create a new .py file. Let’s start with importing ftplib, which we’ll be using to transfer files.

Next, it’s time to configure the connection to the server. We do this by providing ftplib with the server address, ftp username and password.

After creating our new FTP object, we’ll define the remote directory where the transferred file needs to get stored. We immediately navigate into this folder.

The last piece of code we’ll add will read a local file (“a.txt” in this case) and transfer it to the previously defined remote directory.

That’s already it!

It’s kinda straight-forward, but for your convenience I pasted the full code below: