YouTip LogoYouTip

Python3 Os Makedirs

# Python3.x Python3 os.makedirs() Method [![Image 3: Python3 OS File/Directory Methods](#) Python3 OS File/Directory Methods](#) * * * ### Overview The os.makedirs() method is used to recursively create multiple directories. If the creation of a subdirectory fails or it already exists, an OSError exception will be raised. On Windows, Error 183 is the exception error for an existing directory. If the first parameter `path` has only one level, meaning only one directory is created, it is the same as the [mkdir()](#) function. ### Syntax The syntax for the **makedirs()** method is as follows: os.makedirs(name, mode=511, exist_ok=False) ### Parameters * **path** -- The directory to be created 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 if the directory already exists. If `exist_ok` is False (the default), a FileExistsError exception is raised if the target directory already exists; if `exist_ok` is True, no FileExistsError exception is raised if the target directory already exists. ### Return Value This method does not return a value. ### Example The following example demonstrates the use of the makedirs() method: ## Example #!/usr/bin/python3 import os,sys # Directory to be created path ="/tmp/home/monthly/daily" os.makedirs( path,0o777); print("Path has been created") The output of executing the above program is: Path has been created [![Image 4: Python3 OS File/Directory Methods](#) Python3 OS File/Directory Methods](#)
← Python3 Os MkdirPython3 Os Makedirs β†’