Generic selectors
Exact matches only
Search in title
Search in content
wb_sunny

File Transfer Protocol in Python Using the ftplib Module

FTplib Module In Python File Transfer Protocol In Python

In this tutorial, we will learn about what file transfer protocol is and We will implement the functionalities of file transfer protocol in Python using different methods.

What is File Transfer protocol or FTP?

FTP is a network protocol for file transfer between two systems in a network. It works as a client-server architecture in which a client system can connect to a server system with or without authentication and the client can view, retrieve or delete files from the server machine if proper permissions are given.

How to connect to a server using File Transfer Protocol in Python?

To perform different tasks, first we have to import the ftplib module after which we can use functions in the module to implement the functions.

In the following code, we have tried to connect to ftp.ubuntu.com using FTP() function of ftplib module and then we have logged in to the site using login() method. When the connection is established after login, the server returns a welcome message which can be printed using getwelcome() method.

#import module
import ftplib

#define server ftp address
site_addr= "ftp.ubuntu.com"

#make a connection to server
ftp_obj = ftplib.FTP(site_addr)

#login to the server
ftp_obj.login()
print("Connected to "+ site_addr +". Welcome message is:")

#print the welcome message
print(ftp_obj.getwelcome())

#close the connection
ftp_obj.close()

Output:

Connected to ftp.ubuntu.com. Welcome message is:
220 FTP server (vsftpd)

It should be noted that you must close the connection at the end of the program using close() method.

Print the name of Present Working Directory

After logging in to the server, we can print the name of the current working directory using the pwd() method. It is invoked on the object returned by FTP() function and it returns the absolute path to the current working directory.

#import module
import ftplib
#define server ftp address
site_addr= "ftp.ubuntu.com"
#make a connection to server
ftp_obj = ftplib.FTP(site_addr)
#login to the server
ftp_obj.login()

print("Present Working Directory is:")
#get the name of present working directory
present=ftp_obj.pwd()
print(present)

#close the connection
ftp_obj.close()

Output:

Present Working Directory is:
/

Printing the contents of a directory

We can print the contents of present working directory using dir() method on the object returned by FTP() function. dir() method simply prints the contents of present working directory and doesn’t return anything.

#import module
import ftplib
#define server ftp address
site_addr= "ftp.ubuntu.com"
#make a connection to server
ftp_obj = ftplib.FTP(site_addr)
#login to the server
ftp_obj.login()
print("Content of the directory "+ " is:")
#print the content of present working directory
ftp_obj.dir()

#close the connection
ftp_obj.close()

Output:

Content of the directory  is:
drwxr-xr-x   31 997      997          4096 Dec 24 17:47 cdimage
drwxr-xr-x   28 997      997          4096 Dec 24 18:14 cloud-images
drwxr-xr-x    8 997      997          4096 Dec 18 21:56 maas-images
drwxr-xr-x    5 997      997          4096 May 11  2010 old-images
drwxr-xr-x   13 997      997          4096 Dec 24 17:01 releases
drwxr-xr-x    6 997      997          4096 Dec 24 18:19 simple-streams
drwxr-xr-x    7 997      997          4096 Dec 24 17:54 ubuntu
drwxr-xr-x    4 997      997          4096 Oct 01 01:33 ubuntu-cloud-archive
drwxr-xr-x    7 997      997          4096 Dec 24 18:21 ubuntu-ports

Change working directory

We can change the present working directory by invoking cwd() method on the object returned by FTP() function. The relative path of the new directory is given as input to the cwd() method and it doesn’t return any value. We’ll use the print() function to display messages.

#import module
import ftplib
#define server ftp address
site_addr= "ftp.ubuntu.com"

#make a connection to server
ftp_obj = ftplib.FTP(site_addr)
#login to the server
ftp_obj.login()
print("Present Working Directory is:")

#get the name of present working directory
present=ftp_obj.pwd()
print(present)
print("Content of the directory "+ " is:")

#print the content of present working directory
ftp_obj.dir()

#change present working directory to "ubuntu"
ftp_obj.cwd("ubuntu")
print("After Change in directory:")
print("Present Working Directory is:")

#get the name of present working directory
present=ftp_obj.pwd()
print(present)
print("Content of the directory "+ " is:")

#print the content of present working directory
ftp_obj.dir()

#close the connection
ftp_obj.close()

Output:

Present Working Directory is:
/
Content of the directory  is:
drwxr-xr-x   31 997      997          4096 Dec 24 17:47 cdimage
drwxr-xr-x   28 997      997          4096 Dec 24 18:14 cloud-images
drwxr-xr-x    8 997      997          4096 Dec 18 21:56 maas-images
drwxr-xr-x    5 997      997          4096 May 11  2010 old-images
drwxr-xr-x   13 997      997          4096 Dec 24 17:01 releases
drwxr-xr-x    6 997      997          4096 Dec 24 18:24 simple-streams
drwxr-xr-x    7 997      997          4096 Dec 24 17:54 ubuntu
drwxr-xr-x    4 997      997          4096 Oct 01 01:33 ubuntu-cloud-archive
drwxr-xr-x    7 997      997          4096 Dec 24 18:21 ubuntu-ports
After Change in directory:
Present Working Directory is:
/ubuntu
Content of the directory  is:
drwxrwxr-x   37 997      997          4096 Oct 23 11:04 dists
drwxr-xr-x    2 997      997        192512 Dec 24 17:44 indices
-rw-r--r--    1 997      997      20997733 Dec 24 17:44 ls-lR.gz
drwxrwxr-x    6 997      997          4096 Feb 27  2010 pool
drwxr-xr-x    3 997      997          4096 Jun 28  2013 project
lrwxrwxrwx    1 997      997             1 Nov 24  2010 ubuntu -> .

Check the size of a file

We can check the size of a file using size() method of ftplib module. size() method is invoked on the object returned by FTP() function and path to file is given as input to the method. It returns file size in bytes as output.

#import module
import ftplib

#define server ftp address
site_addr= "ftp.ubuntu.com"

#make a connection to server
ftp_obj = ftplib.FTP(site_addr)

#login to the server
ftp_obj.login()
ftp_obj.cwd("ubuntu")

print("Present Working Directory is:")

#get the name of present working directory
present=ftp_obj.pwd()
print(present)
print("Content of the directory "+ " is:")
ftp_obj.dir()

#print size of "ls-lR.gz"
fsize= ftp_obj.size("ls-lR.gz")
print("Size of file ls-lR.gz is:"+ str(fsize))

#close the connection
ftp_obj.close()

Output:

Present Working Directory is:
/ubuntu
Content of the directory  is:
drwxrwxr-x   37 997      997          4096 Oct 23 11:04 dists
drwxr-xr-x    2 997      997        180224 Dec 24 18:43 indices
-rw-r--r--    1 997      997      20988844 Dec 24 18:43 ls-lR.gz
drwxrwxr-x    6 997      997          4096 Feb 27  2010 pool
drwxr-xr-x    3 997      997          4096 Jun 28  2013 project
lrwxrwxrwx    1 997      997             1 Nov 24  2010 ubuntu -> .
Size of file ls-lR.gz is:20988844

Conclusion

In this article, we have seen how to work with file transfer protocol in Python using ftplib module by performing read operations on files at a remote server. Stay tuned for more informative tutorials.

Happy Learning!