Create a directory in Python
The 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. The os
and os.path
modules include many functions to interact with the file system. 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.
There are different methods available in the OS module for creating a director. These are –
Using os.mkdir()
os.mkdir()
method in Python is used to create a directory named path with the specified numeric mode. This method raise FileExistsError
if the directory to be created 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: Use of os.mkdir()
method to create directory/file
import os
directory = "GeeksforGeeks"
parent_dir = "D:/Pycharm projects/"
path = os.path.join(parent_dir, directory)
os.mkdir(path)
print ( "Directory '% s' created" % directory)
directory = "Geeks"
parent_dir = "D:/Pycharm projects"
mode = 0o666
path = os.path.join(parent_dir, directory)
os.mkdir(path, mode)
print ( "Directory '% s' created" % directory)
|
Output:
Directory 'GeeksforGeeks' created
Directory 'Geeks' created
Example #2: Errors while using os.mkdir()
method.
import os
directory = "GeeksForGeeks"
parent_dir = "D:/Pycharm projects/"
path = os.path.join(parent_dir, directory)
os.mkdir(path)
print ( "Directory '% s' created" % directory)
|
Output:
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: Handling error while using os.mkdir()
method.
import os
path = 'D:/Pycharm projects / GeeksForGeeks'
try :
os.mkdir(path)
except OSError as error:
print (error)
|
Output:
[WinError 183] Cannot create a file when that file/
/already exists: 'D:/Pycharm projects/GeeksForGeeks'
Using os.makedirs()
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:
D:/Pycharm projects/GeeksForGeeks/Authors/Nikhil
Suppose we want to create directory ‘Nikhil’ but Directory ‘GeeksForGeeks’ and ‘Authors’ are unavailable in the path. Then os.makedirs()
method will create all unavailable/missing directories in 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: Use of os.makedirs()
method to create directory.
import os
directory = "Nikhil"
parent_dir = "D:/Pycharm projects/GeeksForGeeks/Authors"
path = os.path.join(parent_dir, directory)
os.makedirs(path)
print ( "Directory '% s' created" % directory)
directory = "c"
parent_dir = "D:/Pycharm projects/GeeksforGeeks/a/b"
mode = 0o666
path = os.path.join(parent_dir, directory)
os.makedirs(path, mode)
print ( "Directory '% s' created" % directory)
|
Output:
Directory 'Nikhil' created
Directory 'c' created
Example #2:
import os
directory = "Nikhil"
parent_dir = "D:/Pycharm projects/GeeksForGeeks/Authors"
path = os.path.join(parent_dir, directory)
os.makedirs(path)
print ( "Directory '% s' created" % directory)
|
Output:
Traceback (most recent call last):
File "gfg.py", line 22, in
os.makedirs(path)
File "C:\Users\Nikhil Aggarwal\AppData\Local\Programs\Python/
/ \Python38-32\lib\os.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 while using os.makedirs() method.
import os
directory = "Nikhil"
parent_dir = "D:/Pycharm projects/GeeksForGeeks/Authors"
path = os.path.join(parent_dir, directory)
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)
|
Output:
Directory 'Nikhil' created successfully
Last Updated :
29 Dec, 2020
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...