Python 3 – os.makedirs() Method
In Python programming language, the os
module is used to interact with the operating system. The os.makedirs()
method is a function that allows you to create a directory in a directory tree. This function creates all the intermediate directories in the tree if they do not exist.
Syntax
Below is the syntax of the os.makedirs()
method in Python:
os.makedirs(name, mode=0o777, exist_ok=False)
where:
– name
: the path to the directory that you want to create.
– mode
: (optional) sets the permission of the newly created directory; defaults to 0o777
.
– exist_ok
: (optional) If the directory already exists, it throws a FileExistsError
. If you want this function to silently skip the creation of the folder if it already exists, set exist_ok
to True
.
Creating a Directory Using os.makedirs() Method
The following Python code demonstrates the use of the os.makedirs()
method to create a directory:
import os
# Create a directory `testdir` in the current directory
try:
os.makedirs("testdir")
print("Directory 'testdir' created successfully.")
except FileExistsError:
print("Directory 'testdir' already exists.")
Output:
Directory 'testdir' created successfully.
In the above code snippet, we first import the os
module. Then, we use the os.makedirs()
method to create a new directory testdir
in the current directory. We also catch the FileExistsError
exception if the directory already exists.
Creating a Directory Tree Using os.makedirs() Method
The os.makedirs()
method can create a directory tree for you. The method can create multiple directories at once, even those embedded in parent directories that don’t exist. Here is an example:
import os
# Create a directory tree using os.makedirs() method
try:
os.makedirs("testdir/folder1/folder2/folder3")
print("Directory tree 'testdir/folder1/folder2/folder3' created successfully.")
except FileExistsError:
print("Directory tree 'testdir/folder1/folder2/folder3' already exists.")
Output:
Directory tree 'testdir/folder1/folder2/folder3' created successfully.
In this example, we created a directory tree with testdir
as the root directory and three nested directories within it.
Changing File Permissions Using os.makedirs() Method
You can set the permissions of the newly created directory using the mode
parameter. By default, os.makedirs()
will set its permission to 0o777
. Here is an example of how you can set the mode parameter:
import os
# Create a directory with permissions explicitly set
try:
os.makedirs("testdir/folder1", mode=0o755)
print("Directory 'testdir/folder1' created successfully.")
except FileExistsError:
print("Directory 'testdir/folder1' already exists.")
Output:
Directory 'testdir/folder1' created successfully.
In this code snippet, we set the permission of the newly created directory testdir/folder1
to 0o755
.
Skipping the Already Existing Directory
By default, the os.makedirs()
method throws an exception if the directory already exists. You can change this behavior by setting the exist_ok
parameter to True
. Here is an example:
import os
# Skip creating the directory if it already exists
try:
os.makedirs("testdir", exist_ok=True)
print("Directory 'testdir' created successfully.")
except FileExistsError:
print("Directory 'testdir' already exists.")
Output:
Directory 'testdir' already exists.
In this code snippet, since the testdir
directory already exists, the os.makedirs()
method skipped creating it and printed a message.
Conclusion
In this article, we learned about the os.makedirs()
method of the Python os
module. We learned how to create a directory, create a directory tree, set permissions, and skip creating a directory if it already exists. This method can come in handy when creating directories in your Python scripts.