Blog available for sell
This blog is available for sale. Please 'contact us' if interested.
Advertise with us
ftp script upload   2   31330
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 URL: ftp.dlptest.com
FTP User: dlpuser@dlptest.com
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 = "ftp.dlptest.com"
username = "dlpuser@dlptest.com"
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)
print(login_status)


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.

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

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://ftp.dlptest.com/.


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 - https://www.pythoncircle.com
#

from ftplib import FTP
import os

host = "ftp.dlptest.com"
username = "dlpuser@dlptest.com"
password = "e73jzTRTNqCN9PYAAjjn"

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

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

print(ftp.dir())


Reference:
1. https://docs.python.org/3/library/ftplib.html
ftp script upload   2   31330
2 comments on 'Uploading A File To Ftp Server Using Python'
Login to comment

Jana May 18, 2019, 1:33 p.m.
your login details are not working. Kindly update it
Vibha Oberoi April 25, 2024, 6:02 p.m.
I am getting error of login

Related Articles:
Python program to convert Linux file permissions from octal number to rwx string
Python program to convert Linux file permissions from octal number to rwx string, Linux file conversion python program, Python script to convert Linux file permissions...
Python Script 17: Setting bing image of the day as desktop wallpaper
Python Script to Set bing image of the day as desktop wallpaper, Automating the desktop wallpaper change to bing image of day using python code, changing desktop wallpaper using python, downloading an image using python code, updating the desktop wallpaper daily using python...
Python Script 16: Generating word cloud image of a text using python
word cloud python, generating a word cloud of a text using python code, python code to generate a word cloud image of a text. Using word frequencies to generate a word cloud image using a python script...
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....
DigitalOcean Referral Badge

© 2024-2025 Python Circle   Contact   Sponsor   Archive   Sitemap