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

Python os.makedirs() method

Python os.makedirs() method

Python File methods Python OS file/directory methods


Overview

The os.makedirs() method creates directories recursively.

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, then mkdir() function.

Syntax

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



os.makedirs(path, mode=0o777)

Parameters

Return Value

This method has no return 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 create

path

=

"/tmp/home/monthly/daily"

os

.

makedirs

(

path

,

0755

)

;

print

"Path created"

The output of the above program is:



路径被创建

Python File methods Python OS file/directory methods