ItsMyCode |
The os
module is a built-in utility available in both Python 2 and 3 versions, and it provides functions to interact easily with the operating system. The os and os.path
modules provide various functions to interact with the file system.
Let’s take a look at various ways through which you can create a directory in Python using the os module.
Method 1 – Using os.mkdir()
function
The os.mkdir()
method is used to create a directory in Python. This method will raise _ *FileExistsError * _if the directory is already present in the specified path.
Syntax: os.mkdir(path, mode = 0o777, *, dir_fd = None)
Parameters :
*path: * The location where you need to create a directory. The directory path will be a string object which includes the path and the directory name which needs to be created.
mode (optional): The permission that needs to be set to the newly-created directory. If you do not specify this parameter, then by default, it will set the permission as 0o777
dir_fd (optional): A file descriptor referring to a directory.
The default value will be set as None if you don’t provide any. If you specify the absolute path, then _dir_fd _ is ignored.
Return Value – The os.mkdir()
doesn’t return any value.
Example 1 – Create a directory in Python using os.mkdir()
The os.mkdir()
creates the directory in the specified path if the directory not exists.
# Python program to create directory using os.mkdir() method
import os
# Directory path
dir_path = "C:/Projects/Tryouts/sample"
os.mkdir(dir_path)
print("Directory '% s' created" % dir_path)
# Directory path
dir_path2 = "C:/Projects/Tryouts/sample2"
# mode
mode = 0o666
os.mkdir(dir_path2, mode)
print("Directory '% s' created" % dir_path2)
Output
Directory 'C:/Projects/Tryouts/sample' created
Directory 'C:/Projects/Tryouts/sample2' created
Example 2 – Exception if the directory already exists
The os.mkdir()
method would raise a FileExistsError Exception if the directory in the location specified already exists.
# Python program to create directory using os.mkdir() method
import os
# Directory path
dir_path = "C:/Projects/Tryouts/sample"
os.mkdir(dir_path)
print("Directory '% s' created" % dir_path)
Output
Traceback (most recent call last):
File "c:\Projects\Tryouts\main.py", line 7, in <module>
os.mkdir(dir_path)
FileExistsError: [WinError 183] Cannot create a file when that file already exists: 'C:/Projects/Tryouts/sample'
Method 2 – Using os.makedirs()
method to create a nested directory in Python
The os.makedirs()
method is used to create a directory recursively in Python, which means while making the leaf directory, if there are any intermediate directory is missed, the method os.makedirs()
will create all of them.
Syntax: os.makedirs(path, mode = 0o777, exist_ok = False)
Parameters :
*path: * The location where you need to make a directory. It will be a string object which includes the path and the directory name which needs to be created.
mode (optional): The permission that needs to be set to the newly-created directory. If you do not specify this parameter, then by default, it will set the permission as 0o777
exist_ok (optional): The default value is false, and if the directory exists, then os.makedir()
will raise an FileExistsError.
Return Value – The os.mkdir()
doesn’t return any value.
Example 1 – Create a directory in Python using os.makedirs()
The os.makedirs()
will create the nested directory if the parent directory doesn’t exist in the specified path.
# Python program to create directory using os.makedirs() method
import os
# Directory path
dir_path = "C:/Projects/Tryouts/test/sample/mydir"
os.makedirs(dir_path)
print("Directory '% s' created" % dir_path)
# Directory path
dir_path2 = "C:/Projects/Tryouts/test/sample/mydir2"
# mode
mode = 0o666
os.makedirs(dir_path2, mode)
print("Directory '% s' created" % dir_path2)
Output
Directory 'C:/Projects/Tryouts/test/sample/mydir' created
Directory 'C:/Projects/Tryouts/test/sample/mydir2' created
Example 2 – Exception if the directory already exists
The os.makedirs()
method would raise a FileExistsError Exception if the directory in the location specified already exists.
# Python program to create directory using os.makedirs() method
import os
# Directory path
dir_path = "C:/Projects/Tryouts/test/sample/mydir"
os.makedirs(dir_path)
print("Directory '% s' created" % dir_path)
Output
Traceback (most recent call last):
File "c:\Projects\Tryouts\main.py", line 7, in <module>
os.makedirs(dir_path)
File "C:\Users\user\AppData\Local\Programs\Python\Python39\lib\os.py", line 225, in makedirs
mkdir(name, mode)
FileExistsError: [WinError 183] Cannot create a file when that file already exists: 'C:/Projects/Tryouts/test/sample/mydir'
The post How to Create a Directory in Python? appeared first on ItsMyCode.
Top comments (0)