Python os.makedirs() Method

Python OS File/Directory Methods


Overview

The os.makedirs() method is used to recursively create directories.

If a subdirectory fails to be created or already exists, an OSError exception will be raised. On Windows, Error 183 is the exception error indicating that the directory already exists.

If the first parameter path has only one level, thenmkdir()the function is the same.

Syntax

makedirs()The method syntax is as follows:

os.makedirs(path, mode=0o777)

Parameters

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

  • mode-- Permission mode.

Return Value

This method returns no value.

Example

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

Example

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

import os, sys

# The directory to be created
path = "/tmp/home/monthly/daily"

os.makedirs( path, 0755 );

print "Path was created"

Executing the above program produces the following output:

The path has been created.

Python OS File/Directory Methods

Other Extensions