The mkd() method of FTP class in Python

The mkd() method creates a new directory as per the path provided. It returns the name of the created directory or an error message. The example Python program logs-in to an FTP server and creates a new directory in the root and lists the contents of the root.

Ftp Updated 15 Oct 2019 1 min read

Method Name

mkd

Method Signature

mkd(pathname)

Parameters

Pathname – Path including the name of the directory to be created at the FTP server.

Return Value

  • Upon success, it returns the Name of the directory created or a success message.
  • Upon failure, an exception with FTP error message like 550 Can't create directory: File exists.

Overview

  • The method mkd() creates a new directory in the server as specified by the parameter pathname.

Example

The Python example below logs into an FTP server by providing an user name and password and creates a new directory with the name testdirectory.

python
# Example Python program that creates a new directory in the FTP server

from ftplib import FTP

# Connect to the FTP server by specifying the host name

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

print(ftp.getwelcome());

# Login to the FTP server

ftpResponse = ftp.login(user="dlpuser@dlptest.com", passwd="bbCKucPzfr4b9YXUY7tvsNKyh");

print(ftpResponse);

# Create a new directory

ftpResponse = ftp.mkd("testdirectory");

print(ftpResponse);

# List the directory

ftpResponse = ftp.dir();

print(ftpResponse);

Output

Output
220-#########################################################

220-Please upload your web files to the public_html directory.

220-Note that letters are case sensitive.

220-#########################################################

220 This is a private system - No anonymous login

230-Your bandwidth usage is restricted

230 OK. Current restricted directory is /

testdirectory

drwx------    7 ftptest9   ftptest9        69632 Oct 15 08:58 .

drwx------    7 ftptest9   ftptest9        69632 Oct 15 08:58 ..

-rw-r--r--    1 ftptest9   ftptest9      3162716 Oct 15 08:45 One.txt

-rw-r--r--    1 ftptest9   ftptest9       187680 Oct 15 08:58 Two.txt

-rw-r--r--    1 ftptest9   ftptest9            0 Oct 15 08:54 Three.txt

-rw-r--r--    1 ftptest9   ftptest9        12944 Oct 15 08:57 ax.conf

drwxr-xr-x    2 ftptest9   ftptest9         4096 Oct 15 08:30 xmls

drwxr-xr-x    2 ftptest9   ftptest9         4096 Oct 15 08:55 tests

None