How to List all Files and Directories in FTP Server using Python?
Last Updated :
13 Jan, 2021
FTP ( File Transfer Protocol ) is set of rules that computer follows to transfer files across computer network. It is TCP/IP based protocol. FTP lets clients share files. FTP is less secure because of files are shared as plain text without any encryption across the network.
It is possible using python to retrieve list of file and directories from FTP server using its in-built ftplib module. ftplib is a pre-installed python package, which enables us to implement client side FTP protocol and in order to use it we simply have to import it like any other module. Basic approach to extract list or directories remains the same.
Approach
- Import module
- Connect to host on default port
Syntax:
FTP(host='', user='', passwd='', acct='', timeout=None, source_address=None, *, encoding='utf-8')
- Log in to the server using login() function
Syntax:
login(user='anonymous', passwd='', acct='')
- Retrieve files and directories accordingly.
- Close connection
Method 1: using retrlines()
Files and directories can be listed with retrlines() function. It returns filename, modified time, file size, owner, file type, permissions and Mode.
Syntax:
retrlines(cmd, callback=None)
Passing 'LIST' in this function retrieves files and information about those files.
Program:
Python3
from ftplib import FTP
ftp = FTP('ftp.us.debian.org')
ftp.login()
# changing directory
ftp.cwd('debian')
ftp.retrlines('LIST')
ftp.quit()
Output:
fig: 2
This function could also be user to search for a file or directory. Search query can be entered in between asterisk(*).
Syntax:
ftp.retrlines('LIST *query*')
Program: listing file names starting with "README".
Python3
from ftplib import FTP
ftp = FTP('ftp.us.debian.org')
ftp.login()
# changing directory
ftp.cwd('debian')
ftp.retrlines('LIST *README*')
ftp.quit()
Output:
fig: 3
Method 2: Using dir()
Files and directories can be listed with dir(). It returns filename, modified time, file size, owner, file type, permissions and Mode.
Syntax:
ftp.dir()
Program:
Python3
from ftplib import FTP
ftp = FTP('ftp.us.debian.org')
ftp.login()
# changing directory
ftp.cwd('debian')
ftp.dir()
ftp.quit()
Output:
fig: 4
Method 3: Using nlst()
Files and directories can be listed with nlst(). It returns name of files and directories of list type.
Syntax:
ftp.nlst()
Program:
Python3
from ftplib import FTP
ftp = FTP('ftp.us.debian.org')
ftp.login()
# changing directory
ftp.cwd('debian')
ftp.nlst()
ftp.quit()
Output:
fig: 5
Similar Reads
How to Download and Upload Files in FTP Server using Python? Prerequisite: FTP, ftplib Here, we will learn how to Download and Upload Files in FTP Server Using Python. Before we get started, first we will understand what is FTP. FTP(File Transfer Protocol) File Transfer Protocol(FTP) is an application layer protocol that moves files between local and remote f
3 min read
Python List All Files In Directory And Subdirectories Listing all files in a directory and its subdirectories is a common task in Python, often encountered in file management, data processing, or system administration. As developers, having multiple approaches at our disposal allows us to choose the most suitable method based on our specific requiremen
5 min read
List all files of certain type in a directory using Python In python, there are several built-in modules and methods for file handling. These functions are present in different modules such as os, glob, etc. This article helps you find out many of the functions in one place which gives you a brief knowledge about how to list all the files of a certain type
3 min read
How to create a list of files, folders, and subfolders in Excel using Python ? In this article, we will learn How to create a list of Files, Folders, and Sub Folders and then export them to Excel using Python. We will create a list of names and paths using a few folder traversing methods explained below and store them in an Excel sheet by either using openpyxl or pandas module
12 min read
How to print all files within a directory using Python? The OS module is one of the most popular Python modules for automating the systems calls and operations of an operating system. With a rich set of methods and an easy-to-use API, the OS module is one of the standard packages and comes pre-installed with Python. In this article, we will learn how to
3 min read