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:

 

3 Comments

  1. Yeah, this code working. But I have a question. How can I see a progress bar while file uploads? I visited to many web-sites and have no answer about this. Can you help me?

  2. Your the best in coding and providing best examples … it’s really helpful …. I have same requirement tried as bellow …. but getting error ….

    server = ‘server_name’
    username = ‘mahesh’
    password = ‘#####’
    ftp_connection = ftplib.FTP(server, username, password)
    remote_path = “/abc/test/mahesh/”
    ftp_connection.cwd(remote_path)
    fh = open(“C:/Users/Mahesh/Desktop/sample_file_test.txt”, ‘rb’)
    ftp_connection.storbinary(‘STOR sample_file_test.txt’, fh)
    fh.close()

    TimeoutError: [WinError 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond

    could you please help me on resolving this issue

Leave a Reply

Your email address will not be published. Required fields are marked *

Protected with IP Blacklist CloudIP Blacklist Cloud