Change language

Python | os.makedirs () method

|

All functions in the os module raise OSError for invalid or unreachable file names and paths or other arguments that are of the correct type but are not accepted by the operating system.

os.makedirs () in Python is used to create a directory recursively. This means that when creating the target directory, if any intermediate tier directory is missing, os.makedirs() will create them all. 
For example, consider the following path:

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

Suppose we want to create the "ihritik" directory, but the " GeeksForGeeks "and" Credits "are not available while in transit. Then os.makedirs() will create the entire inaccessible / missing directory at the specified path. First GeeksForGeeks and Authors will be created, then the 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.

Return Type: This method does not return any value.

Code # 1: Using the os.makedirs () method to create a directory

# Python program to explain the os.makedirs () method

 
# module import os

import os

  
# Leaf directory

directory = "ihritik"

 
# Parent directories

parent_di r = "/ home / User / Documents / GeeksForGeeks / Authors"

 
# Path

path = os.path.join ( (parent_dir, directory)

  
# Create directory
# & # 39; ihritik & # 39;
os.makedirs (path)

print ( "Directory’% s’ created " % directory)

 
# Directories & # 39; GeeksForGeeks & # 39; and & # 39; Authors & # 39; will
# be created too
# if it doesn’t exist

 

 

 
# Leaf directory

directory = " c "

  
# Parent directories

parent_dir = " / home / User / Documents / Python.Engineering / a / b "

  
# Mode

mode = 0o666

 

path = os.path.join ( (parent_dir, directory)

 
# Create directory
# & # 39; c & # 39;

  
os.makedirs (path, mode)

print ( "Directory’% s’ created " % directory)

  

  
# & # 39; GeeksForGeeks & # 39 ;, & # 39; a & # 39; and & # 39; b & # 39;
# will also be generated if
# does not exist

 
# If any of the intermediate level
# no catalog
# os.makedirs () method
# create them

  
The # os.makedirs () method can be
# used to create a directory tree

Exit :

 Directory ’ihritik’ created Directory’ c’ created 

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

# Python program to explain met os.makedirs () code

 
# import of the os module

import os

 
The # os.makedirs () method will call
# OSE error if directory
# already exists to create

 

 
# Directory

directory = "ihritik"

 
# Path to parent directory

parent_dir = "/ home / User / Documents / GeeksForGeeks"

  
# Path

path = os.path.join ( (parent_dir, directory)

 
# Create directory
# & # 39; ihritik & # 39;
os.makedirs (path)

print ( " Directory ’% s’ created" % directory)

Exit :

 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: Error handling when using the os.makedirs () method

 

# Python program to explain the os.makedirs () method

 
# import of the os module

import os

 
The # os.makedirs () method will call
# OSE error if directory
# already exists to create
# But it may be suppressed
# setting parameter value
# exist_ok as True

 
# Directory

directory = "ihritik"

 
# Path to parent directory

parent_dir = "/ home / ihritik / Desktop / GeeksForGeeks"

 
# Path

path = os.path.join ( (parent_dir, directory)

 
# Create directory
# & # 39; ihritik & # 39 ;

try :

  os.makedirs (path, exist_ok = True )

print (< / code> "Directory’% s’ created successfully " % directory)

except OSError as error:

print ( " Directory ’% s’ can not be created " )

  

 
# Setting Existence_of_s as True
# an error already thrown
# existing directory may be suppressed
# but another OSError may be raised
# because of another error like
# invalid path

Exit:

 Directory ’ihritik’ created successfully 

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

Shop

Best laptop for Sims 4

$

Best laptop for Zoom

$499

Best laptop for Minecraft

$590

Best laptop for engineering student

$

Best laptop for development

$

Best laptop for Cricut Maker

$

Best laptop for hacking

$890

Best laptop for Machine Learning

$950

Latest questions

NUMPYNUMPY

psycopg2: insert multiple rows with one query

12 answers

NUMPYNUMPY

How to convert Nonetype to int or string?

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Javascript Error: IPython is not defined in JupyterLab

12 answers

News

Wiki

Python OpenCV | cv2.putText () method

numpy.arctan2 () in Python

Python | os.path.realpath () method

Python OpenCV | cv2.circle () method

Python OpenCV cv2.cvtColor () method

Python - Move item to the end of the list

time.perf_counter () function in Python

Check if one list is a subset of another in Python

Python os.path.join () method