Saturday, November 16, 2024
Google search engine
HomeLanguagesPython | os.makedirs() method

Python | os.makedirs() method

OS module in Python provides functions for interacting with the operating system. OS comes under Python’s standard utility modules. This module provides a portable way of using operating system dependent functionality.
All functions in os module raise OSError in the case of invalid or inaccessible file names and paths, or other arguments that have the correct type but are not accepted by the operating system. 
os.makedirs() method in Python is used to create a directory recursively. That means while making leaf directory if any intermediate-level directory is missing, os.makedirs() method will create them all.
For example consider the following path:
 

/home/User/Documents/GeeksForGeeks/Authors/ihritik

Suppose we want to create directory ‘ihritik’ but Directory ‘GeeksForGeeks’ and ‘Authors’ are unavailable in the path. Then os.makedirs() method will create all unavailable/missing directory in the specified path. ‘GeeksForGeeks’ and ‘Authors’ will be created first then ‘ihritik’ directory will be created. 
 

Syntax: os.makedirs(path, mode = 0o777, exist_ok = False) 
Parameter: 
path: A path-like object representing a file system path. A path-like object is either a string or bytes object representing a path.
mode (optional) : A Integer value representing mode of the newly created directory..If this parameter is omitted then the default value Oo777 is used.
exist_ok (optional) : A default value False is used for this parameter. If the target directory already exists an OSError is raised if its value is False otherwise not. For value True leaves directory unaltered. 
Return Type: This method does not return any value.
 

Code #1: Use of os.makedirs() method to create directory
 

Python3




# Python program to explain os.makedirs() method
   
# importing os module
import os
 
# Leaf directory
directory = "ihritik"
 
# Parent Directories
parent_dir = "/home/User/Documents/GeeksForGeeks/Authors"
 
# Path
path = os.path.join(parent_dir, directory)
 
# Create the directory
# 'ihritik'
os.makedirs(path)
print("Directory '%s' created" %directory)
 
# Directory 'GeeksForGeeks' and 'Authors' will
# be created too
# if it does not exists
 
 
 
# Leaf directory
directory = "c"
 
# Parent Directories
parent_dir = "/home/User/Documents/Lazyroar/a/b"
 
# mode
mode = 0o666
 
path = os.path.join(parent_dir, directory)
 
# Create the directory
# 'c'
  
os.makedirs(path, mode)
print("Directory '%s' created" %directory)
 
 
# 'GeeksForGeeks', 'a', and 'b'
# will also be created if
# it does not exists
 
# If any of the intermediate level
# directory is missing
# os.makedirs() method will
# create them
 
# os.makedirs() method can be
# used to create a directory tree 


Output:
 

Directory 'ihritik' created
Directory 'c' created

Code #2: Errors while using os.makedirs() method
 

Python3




# Python program to explain os.makedirs() method
   
# importing os module
import os
 
# os.makedirs() method will raise
# an OSError if the directory
# to be created already exists
 
    
# Directory
directory = "ihritik"
 
# Parent Directory path
parent_dir = "/home/User/Documents/GeeksForGeeks"
 
# Path
path = os.path.join(parent_dir, directory)
 
# Create the directory
# 'ihritik'
os.makedirs(path)
print("Directory '%s' created" %directory)


Output:
 

Traceback (most recent call last):
  File "makedirs.py", line 21, in 
    os.makedirs(path)
  File "/usr/lib/python3.6/os.py", line 220, in makedirs
    mkdir(name, mode)
FileExistsError: [Errno 17] File exists: '/home/User/Documents/GeeksForGeeks/ihritik'

Code #3: Handling errors while using os.makedirs() method
 

Python3




# Python program to explain os.makedirs() method
   
# importing os module
import os
 
# os.makedirs() method will raise
# an OSError if the directory
# to be created already exists
# But It can be suppressed by
# setting the value of a parameter
# exist_ok as True
    
# Directory
directory = "ihritik"
 
# Parent Directory path
parent_dir = "/home/ihritik/Desktop/GeeksForGeeks"
 
# Path
path = os.path.join(parent_dir, directory)
 
# Create the directory
# 'ihritik'
try:
    os.makedirs(path, exist_ok = True)
    print("Directory '%s' created successfully" %directory)
except OSError as error:
    print("Directory '%s' can not be created")
 
 
# By setting exist_ok as True
# error caused due already
# existing directory can be suppressed
# but other OSError may be raised
# due to other error like
# invalid path name


Output:
 

Directory 'ihritik' created successfully

Reference: https://docs.python.org/3/library/os.html
 

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments