Python Django Live Projects for sell
We have few Python Django live projects which are available for sell. Please visit this link for more details.
Advertise with us
ftp script upload   1   13257
Uploading a file to FTP server using Python

In this article we will see how to connect to, login and upload a file to FTP server using python.

We will require a publicly available FTP server to test our code. You can use below details for same.

FTP User:
Password: e73jzTRTNqCN9PYAAjjn

If above details are not working or are outdated, let us know by commenting below so that we can update the article. Meanwhile you can search other FTP server details publicly available over the Internet.

We will use ftplib python module.

from ftplib import FTP

Define host, username and password.

host = ""
username = ""
password = "e73jzTRTNqCN9PYAAjjn"
since these details are publicly availabe, we need not to worry about storing them in env or in config file.

Create a connection.

ftp = FTP(host=host)

Login to FTP server.

login_status = ftp.login(user=username, passwd=password)

Now create a dummy file in your current directory.

echo 'Hi Rana' > rana_ftp.txt

Print the content of current directory on FTP server and upload the txt file.

fp = open("rana_ftp.txt", 'rb')
ftp.storbinary('STOR %s' % os.path.basename("rana_ftp.txt"), fp, 1024)

Print the content of current directory on FTP again after uploading file. 

ftp file upload python 1

You can verify via browser by visiting the link

ftp file upload python 1 1

If you need to upload the file in some other directory, change to that directory using ftp.cwd('dirname').

Here we have uploaded the file to upload directory.

ftp file upload python 2

ftp file upload python 2 2

Complete code is below.

# Sample python program showing FTP connection and
# how to upload any file to a FTP server
# Author -

from ftplib import FTP
import os

host = ""
username = ""
password = "e73jzTRTNqCN9PYAAjjn"

# connect to host on default port i.e 21
ftp = FTP(host=host, user=username, passwd=password)
login_status = ftp.login()

# change directory to upload
# print the content of directory
fp = open("rana_ftp.txt", 'rb')
# upload file
ftp.storbinary('STOR %s' % os.path.basename("rana_ftp.txt"), fp, 1024)


ftp script upload   1   13257

Related Articles:
Python Script 13: Generating ascii code from Image
Generating ascii art from image, converting colored image to ascii code, python script to convert image to ascii code, python code to generate the ascii image from jpg image....
Python Script 8: Validating Credit Card Number - Luhn's Algorithm
Validating credit card number using Luhn' Algorithm, Verifying Credit and Debit card using python script, Python code to validate the credit card number, Luhn' algorithm implementation in Python...
Python Script 5: How to find most popular technologies on Stackoverflow
How to find most popular technology on stackoverflow by crawling the stackoverflow site using python. Using python beautifulsoup to crawl web pages on stackoverflow. Python code to crawl stackoverflow, crawling stackoverflow for tags, python script to fetch data from stackoverflow....
How to upload an Image file in Django
This article explains the simple steps of uploading and storing an image in Django application, After storing the image, how to use it in Django template or emails, Uploading a file in Django, Storing image in Django model, Uploading and storing the image in Django model, HTML for multipart file upload...

1 thought on 'Uploading A File To Ftp Server Using Python'
Jana :
your login details are not working. Kindly update it

Leave a comment:

*All Fields are mandatory. **Email Id will not be published publicly.

Please subscribe to get the latest articles in your mailbox.

© 2017-2020 Python Circle   Contact   Sponsor   Archive   Sitemap