Create directory in Python

Python Methods and Functions

Different methods are available in the OS module for creating a director. These —

Using os.mkdir ()

os.mkdir () in Python is used to create a directory with named path with the specified numeric mode. This method FileExistsError if the directory to create already exists.

Syntax: os.mkdir (path, mode = 0o777, *, dir_fd = None)

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 directory to be created. If this parameter is omitted then default value Oo777 is used.
dir_fd (optional): A file descriptor referring to a directory. The default value of this parameter is None.
If the specified path is absolute then dir_fd is ignored.

Note: The '*' in parameter list indicates that all following parameters (Here in our case 'dir_fd') are keyword-only parameters and they can be provided using their name, not as positional parameter.

Return Type: This method does not return any value.

Example # 1: Using the os.mkdir () method to create a directory / file

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

  
# import of the os module

import os

 
# Directory

directory = "GeeksforGeeks"

 
# Path to parent directory

parent_dir = "D: / Pycharm projects /"

 
# Path

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

 
# Create directory
# & # 39; GeeksForGeeks & # 39; in
# & # 39; / home / User / Documents & # 39;
os.mkdir (path)

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

 
# Directory

directory = " Geeks "

  
# Path to parent directory

parent_dir = "D: / Pycharm projects"

 
# Mode

mode = 0o666

  
# Path

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

  
# Create directory
# & # 39; GeeksForGeeks & # 39; in
# & # 39; / home / User / Documents & # 39;
# with mode 0o666
os.mkdir (path, mode)

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

Output:

 Directory 'GeeksforGeeks' created Directory' Geeks' created 

Example # 2: Errors when using os.mkdir () .

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

 
# import of the os module

import os 

 
# Directory

directory = "GeeksForGeeks"

 
# Path to parent directory

parent_dir = "D: / Pycharm projects /"

 
# Path

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

 
# Create directory
# & # 39; GeeksForGeeks & # 39; in
# & # 39; / home / User / Documents & # 39;
os.mkdir (path) 

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

 
# if the directory / file that
# should already be created
# exists then & # 39; FileExistsError & # 39;
# will be called by the os.mkdir () method

 
# Similarly, if the path is specified
# not allowed Error & # 39; FileNotFoundError & # 39;
# will be raised

Exit :

 Traceback (most recent call last): File "gfg.py", line 18, in os.mkdir (path) FileExistsError: [WinError 183] Cannot create a file when that file / / already exists: 'D: / Pycharm projects / GeeksForGeeks '

Example # 3: Error handling when using os.mkdir () .

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

 
# import the os module

import os 

 
# path

path = 'D: / Pycharm projects / GeeksForGeeks'

  
# Create directory
# & # 39; GeeksForGeeks & # 39; in
# & # 39; / home / User / Documents & # 39;

try

os.mkdir ( path) 

except OSError as error: 

print (error) 

Exit:

 [WinError 183 ] Cannot create a file when that file / / already exists: 'D: / Pycharm projects / GeeksForGeeks' 

makedirs> Using os.makedirs ()

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

 D: / Pycharm projects / GeeksForGeeks / Authors / Nikhil 

Suppose we want to create a Nikhil directory, but the GeeksForGeeks directory "And" Authors "are not available en route. Then os.makedirs () will create all inaccessible / missing directories at the specified path. GeeksForGeeks and Authors will be created first, then Nikhil 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.

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

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

 
# import module os

import os 

 
# Leaf directory

directory = "Nikhil"

 
# Parent directories

parent_dir = " D: / Pycharm projects / GeeksForGeeks / Authors "

 
# Path

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

 
# Create directory
# & # 39; Nikhil & # 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 = " D: / Pycharm projects / Python.Engineering / a / b "

  
# Mode

mode = 0o666

 

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

 
# Create a 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 'Nikhil' created Directory' c' created 

Example # 2:

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

 
# import of the os module

import os 

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

 

 
# Directory

directory = "Nikhil"

 
# Path to parent directory

parent_dir = "D: / Pycharm projects / GeeksForGeeks / Authors "

  
# Path

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

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

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

Exit :

 Traceback (most recent call last): File "gfg.py", line 22, in os. makedirs (path) File "C: UsersNikhil AggarwalAppDataLocalProgramsPython / / Python38-32libos.py", line 221, in makedirs mkdir (name, mode) FileExistsError: [WinError 183] Cannot create a file when that / / file already exists: 'D : / Pycharm projects / GeeksForGeeks / Authors  Nikhil' 

Example # 3: Handling errors 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
# for creation already exists
# But this can be suppressed
# setting a parameter value
# exist_ok as True

 
# Directory

directory = "Nikhil"

 
# Path to parent directory

parent_dir = "D: / Pycharm projects / GeeksForGeeks / Authors"

 
# Path

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

 
# Create directory
# & # 39; Nikhil & # 39;

try :

os.makedirs (path, exist_ok = True )

  print ( " Directory '% s' created successfully " % directory)

except OSError as error:

print ( "Directory'% s' can not be created " % directory)

 
# By setting Existence_ok as True
# already caused error
# existing directory may be suppressed
# but another OSError may be raised
# due to another errors like
# invalid path

Exit :

 Directory 'Nikhil' created successfully 




Get Solution for free from DataCamp guru