Python3 os.makedirs() Method
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
-
path -- The directory to create recursively; it can be a relative or absolute path.
-
mode -- The permission mode; the default mode is 511 (octal)..
- exist_ok: Whether to raise an exception when the directory exists. If exist_ok is False (the default), a FileExistsError is raised when the target directory already exists; if exist_ok is True, no FileExistsError is raised in that case.
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
)
;
(
"Path created"
)
The output of the above program is:
路径被创建