Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Python3 os.makedirs() Method

Python3 os.makedirs() Method

Python3 OS File/Directory Methods Python3 OS File/Directory Methods


Overview

The os.makedirs() method recursively creates multiple levels of directories.

If creating a subdirectory fails or it already exists, an OSError is raised; on Windows Error 183 is the error for a directory that already exists.

If the first parameter path has only one level, that is, only one directory is created, it is the same as the mkdir() function.

Syntax

The **makedirs()** method syntax is as follows:


os.makedirs(name, mode=511, exist_ok=False)

Parameters

Return Value

This method has no return value.

Example

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

Example

#!/usr/bin/python3

import

os

,

sys

The directory to create

path

=

"/tmp/home/monthly/daily"

os

.

makedirs

(

path

,

0o777

)

;

print

(

"Path created"

)

The output of the above program is:


路径被创建

Python3 OS File/Directory Methods Python3 OS File/Directory Methods