Python - FTP

Neha Kumawat

a year ago

FTP or File Transfer Protocol is a well-known network protocol used to transfer files between computers in a network. It is created on a client-server architecture and can be used along with user authentication. It can also be used without authentication but that will be less secure. FTP connection maintains a current working directory and other flags and each transfer requires a secondary connection through which the data is transferred. Most common web browsers can retrieve files hosted on FTP servers.
Python FTP | Insideaiml
Python FTP | Insideaiml

The Methods in FTP class

In python, we use the module ftplib which has the below-required methods to list the files as we will transfer the files.
Methods in FTP class | Insideaiml
Methods in FTP class | Insideaiml
Below are the examples of some of the above methods.

Listing the Files

The below example uses anonymous login to the FTP server and lists the content of the current directory. It treats through the name of the files and directories and stores them as a list. Then prints them out.

import ftplib
 
ftp = ftplib.FTP("ftp.nluug.nl")
ftp.login("anonymous", "ftplib-example-1")
 
data = []
 
ftp.dir(data.append)
 
ftp.quit()
 
for line in data:
    print("-", line)
When we run the above program, we get the following output

- lrwxrwxrwx    1 0        0               1 Nov 13  2012 ftp -> .
- lrwxrwxrwx    1 0        0               3 Nov 13  2012 mirror -> pub
- drwxr-xr-x   23 0        0            4096 Nov 27  2017 pub
- drwxr-sr-x   88 0        450          4096 May 04 19:30 site
- drwxr-xr-x    9 0        0            4096 Jan 23  2014 vol

Changing the Directory

The below program uses the CWD method available in the ftplib module to change the directory and then fetch the required content.

import ftplib
 
ftp = ftplib.FTP("ftp.nluug.nl")
ftp.login("anonymous", "ftplib-example-1")
 
data = []
 
ftp.cwd('/pub/')          #change directory to /pub/
ftp.dir(data.append)
 
ftp.quit()
 
for line in data:
    print("-", line)
When we run the above program, we get the following output

- lrwxrwxrwx    1 504      450            14 Nov 02  2007 FreeBSD -> os/BSD/FreeBSD
- lrwxrwxrwx    1 504      450            20 Nov 02  2007 ImageMagick -> graphics/ImageMagick
- lrwxrwxrwx    1 504      450            13 Nov 02  2007 NetBSD -> os/BSD/NetBSD
- lrwxrwxrwx    1 504      450            14 Nov 02  2007 OpenBSD -> os/BSD/OpenBSD
- -rw-rw-r--    1 504      450           932 Jan 04  2015 README.nluug
- -rw-r--r--    1 504      450          2023 May 03  2005 WhereToFindWhat.txt
- drwxr-sr-x    2 0        450          4096 Jan 26  2008 av
- drwxrwsr-x    2 0        450          4096 Aug 12  2004 comp


Fetching the Files

After getting the list of files as shown above, we can fetch a specific file by using the getfile method. This method moves a copy of the file from the remote system to the local system from where the ftp connection was initiated.

import ftplib
import sys
 
def getFile(ftp, filename):
    try:
        ftp.retrbinary("RETR " + filename ,open(filename, 'wb').write)
    except:
        print "Error"
 
 
ftp = ftplib.FTP("ftp.nluug.nl")
ftp.login("anonymous", "ftplib-example-1")
 
ftp.cwd('/pub/')          #change directory to /pub/
getFile(ftp,'README.nluug')
 
ftp.quit()

When we run the above program, we find the file README.nlug to be present in the local system from where the connection was initiated.
To learn more about python, visit the InsideAIML page.
I hope you enjoyed reading this article and finally, you came to know about Python - FTP
For more such blogs/courses on data science, machine learning, artificial intelligence and emerging new technologies do visit us at InsideAIML.
Thanks for reading…
Happy Learning…

Submit Review