Python os.mkdir() Method

Python OS File/Directory Methods


Overview

The os.mkdir() method is used to create a directory (single-level directory) with a numeric permission mode. The default mode is 0777 (octal).

If the directory has multiple levels, only the last level is created. If any parent directory of the last-level directory does not exist, an OSError will be raised.

Syntax

mkdir()The method syntax format is as follows:

os.mkdir(path[, mode])

Parameters

  • path-- The directory to be created, which can be a relative or absolute path.

  • mode-- The numeric permission mode to be set for the directory.

Return Value

This method has no return value.

Example

The following example demonstrates the use of the mkdir() method:

Example

#!/usr/bin/python
# -*- coding: UTF-8 -*-

import os, sys

# Directory to be created
path = "/tmp/home/monthly/daily/hourly"

os.mkdir( path, 0755 );

print Directory created

The output of executing the above program is:

Directory has been created

Python OS File/Directory Methods

Other Extensions