Python FTP Library Directory Listing

Python FTP Library Directory Listing

                Directory Listing In Python FTP Library(ftplib)
In the last article we were introduced to the Python ftplib library.
We learnt about how to connect to a FTP server and also learnt about different methods like ftplib.FTP() , connect() , getwelcome() and quit()methods along with the example codes and their respective outputs.

In this article we will learn about DIRECTORY LISTING of FTP server using ftplib library.

What is Directory ?:
A Directory is another name for a folder. File systems use Directories to organize files within a storage devices. In FTP server, the directory looks similar to that of the directories we see in our computer system. They look like as shown below.

The above picture is a part of the FTP server Directory of "". You can visit the link to see the the other directories.

Directory Listing using ftplib:
The dir() method produces a directory listing of a FTP server. (We will use the "" server in the examples).


The example connects to the host and retrieves the directory listing of the initial landing directory.


1) When the login() method has no parameters; we connect to the anonymous account of the FTP site. 
2) The login(user = "anonymous", passwd = '', acct = '') has 3 arguments i.e. user, passwd           and acct. This is used to login as the given user. The passwd and acct are optional and default to empty           strings.
3) If no user is specified, it defaults to "anonymous". If user is "anonymous" then the passwd is                             "anonymous@".  
4) This method should not be called when user and host are given when the instance was created.
5) Most FTP commands are allowed after the user is logged in to the server.

1) The dir() method produces a directory listing as a List and the loop prints the Directory in the                      standard output.
2) The optional argument is a directory to list (default is the current server directory).

1) The set of all exceptions (as a tuple) is returned in case the FTP instances raise as a result of problems            with the FTP connections.


The above output shown is the Directory of the FTP server "".

We learnt about how to list the directory of the FTP server and the different methods associated with it. We also learnt about the different possible errors that the program may raise and in which conditions.

In the next article, We will learn about the different FTP COMMANDS that are available in the Python 
ftplib library.