The nlst() method of FTP class in Python

The nlst() method of FTP class in Python returns the names of the elements of a specified directory as a Python list. When no directory is specified the current working directory is assumed.

Ftp Updated 15 Oct 2019 1 min read

Method Name

nlst

Method Signature

nlst(argument[,…])

Parameters

The optional argument is the name (i.e., path) of the directory. If, no value is passed for the optional argument, current working directory for the logged in user is assumed as the value.

Return Value

List of file names in the directory specified by the argument.

Overview

  • The nlst() method returns the names of the elements present in a directory.
  • The nlst() method internally sends and NLST command to the FTP server and returns the response as a list of names present in the specified directory.
  • Unlike mlsd() and dir() methods, apart from the names of the elements the NLST command does not return any other information on the elements.
  • The nlst() transfers the text lines containing the directory elements based on the active/passive mode of file transfer in ASCII mode.

Example

python
# Example Python program that uses NLST command

from ftplib import FTP

# Instantiate an FTP object and connect to the FTP server

ftp = FTP(host="somegoodftpserver.com");

# Login to the FTP server

ftp.login(user="poweruser@ somegoodftpserver.com", passwd="xcvTYGhNft54FG");

# Issue NLST

for name in ftp.nlst():

    print(name);

Output

Output
.

..

One.txt

Two.txt

ThreeDir