Python Script Uploading files via FTP

I would like to make a script to upload a file to FTP.

How would the login system work? I’m looking for something like this:


And any other sign in credentials.

Here is Solutions:

We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.

Solution 1

Use ftplib, you can write it like this:

import ftplib
session = ftplib.FTP('','USERNAME','PASSWORD')
file = open('kitten.jpg','rb')                  # file to send
session.storbinary('STOR kitten.jpg', file)     # send the file
file.close()                                    # close file and FTP

Use ftplib.FTP_TLS instead if you FTP host requires TLS.

To retrieve it, you can use urllib.retrieve:

import urllib 

urllib.urlretrieve('ftp://server/path/to/file', 'file')


To find out the current directory, use FTP.pwd():

FTP.pwd(): Return the pathname of the current directory on the server.

To change the directory, use FTP.cwd(pathname):

FTP.cwd(pathname): Set the current directory on the server.

Solution 2

ftplib now supports context managers so I guess it can be made even easier

from ftplib import FTP
from pathlib import Path

file_path = Path('kitten.jpg')

with FTP('', 'USER', 'PWD') as ftp, open(file_path, 'rb') as file:
        ftp.storbinary(f'STOR {}', file)

No need to close the file or the session

Solution 3

You will most likely want to use the ftplib module for python

 import ftplib
 ftp = ftplib.FTP()
 host = ""
 port = 21
 ftp.connect(host, port)
 print (ftp.getwelcome())
      print ("Logging in...")
      ftp.login("yourusername", "yourpassword")
     "failed to login"

This logs you into an FTP server. What you do from there is up to you. Your question doesnt indicate any other operations that really need doing.

Solution 4

Try this:

#!/usr/bin/env python

import os
import paramiko 
ssh = paramiko.SSHClient()
ssh.connect('hostname', username="username", password="password")
sftp = ssh.open_sftp()
localpath = '/home/e100075/python/ss.txt'
remotepath = '/home/developers/screenshots/ss.txt'
sftp.put(localpath, remotepath)

Solution 5

To avoid getting the encryption error you can also try out below commands

ftp = ftplib.FTP_TLS("")
ftp.login("username", "password")
file = open("filename", "rb")
ftp.storbinary("STOR filename", file)

ftp.prot_p() ensure that your connections are encrypted

Solution 6

I just answered a similar question here
IMHO, if your FTP server is able to communicate with Fabric please us Fabric. It is far better than doing raw ftp.

I have an FTP account from so I am not sure if this will work for other FTP accounts.


from fabric.api import run, env, sudo, put

env.user = 'username'
env.hosts = ['ftp_host_name',]     # such as

def copy():
    # assuming i have in the same directory as this script
    put('', '/www/public/')

save the file as and run fab copy locally.

[email protected]:~$ fab copy2
[] Executing task 'copy2'
[] Login password: 
[] put: -> /www/public/

Disconnecting from done.

Once again, if you don’t want to input password all the time, just add

env.password = 'my_password'

Solution 7

You can use the below function. I haven’t tested it yet, but it should work fine. Remember the destination is a directory path where as source is complete file path.

import ftplib
import os

def uploadFileFTP(sourceFilePath, destinationDirectory, server, username, password):
    myFTP = ftplib.FTP(server, username, password)
    if destinationDirectory in [name for name, data in list(remote.mlsd())]:
        print "Destination Directory does not exist. Creating it first"
    # Changing Working Directory
    if os.path.isfile(sourceFilePath):
        fh = open(sourceFilePath, 'rb')
        myFTP.storbinary('STOR %s' % f, fh)
        print "Source File does not exist"

Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from or, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply