Juggling between coding languages? Let our Code Converter help. Your one-stop solution for language conversion. Start now!
FTP (File Transfer Protocol) is the standard network protocol that enables computers to transfer files across the network. In this tutorial, you will learn how you can connect to an FTP server and list all files and directories on it, you will also get familiar with Python's built-in ftplib module and some of its methods.
The ftplib
library comes pre-installed with Python, so if you have Python installed on your machine, you're good to go. Open up a new Python file and follow along, let's import the necessary module for the tutorial:
In a regular FTP server, you need credentials (username and password) in order to properly log into one, but for this tutorial, we gonna use the FTP server of the University of Edinburgh which enables users to log in anonymously:
Below are utility functions that will help us later print our list of files and directories:
The get_size_format()
function was grabbed from this tutorial, it basically converts the size of files from bytes into a more human-readable format, such as 1.3MB, 103.5KB, etc. The get_datetime_format()
function also converts the date time into a more readable format.
Now, let's connect to our server using the FTP()
client class:
When writing the code of this tutorial, I've encountered some problems working with nonlatin characters, as Python uses ISO-8859-1 as default encoding, as a result, let's change the encoding to UTF-8:
Now that we are inside the server, let's print the welcome message that is sent by the server once we're connected:
Here is the output for this server:
Let's start calling some commands, the first method we gonna use is cwd(), which changes the current working directory, since we're in the root directory, let's change to some directory that has some files inside it:
Listing files and directories:
Here is a part of the output:
Quite similar to the output provided by the ls
command. However, this uses the FTP's LIST
command which is obsolete by now. Also, as you may already notice, it does not return any value, it just prints to the screen the directories and files in the current working directory.
Another alternative is to use NLST
command:
Output:
But, as you may see, the NLST
command returns only the names of files and directories, nothing else, we want something that provides us the list of names as well as their metadata such as permissions, size, date of last modification, etc.
Here we use the MLSD
command that comes to the rescue:
We used the mlsd() method that calls FTP's MLSD
command, it returns a tuple that contains the file name and the file metadata, we extracted everything and printed them to the screen. Notice I used TYPE I
command to change the type of transfer into a binary image, this is because size() will raise an exception if it's not the case.
We also used our previously defined function get_datetime_format() to convert the date returned by the FTP server into a more human-readable format, here is a truncated output of the above recipe:
MLSD
command is the current FTP standard of formatting directory listings, it was introduced on RFC 3659.
Finally, after working with the FTP server, it's time to quit and close the connection:
Alright, that's it for the tutorial. You shouldn't use LIST
command (using dir() method in Python) now, MLSD
is the way to go, even though some FTP servers still don't support MLSD
, NLST
command is still an alternative.
Related: How to Download and Upload Files in FTP Server using Python.
Happy Coding ♥
Ready for more? Dive deeper into coding with our AI-powered Code Explainer. Don't miss it!
View Full Code Explain My Code
Got a coding query or need some guidance before you comment? Check out this Python Code Assistant for expert advice and handy tips. It's like having a coding tutor right in your fingertips!