In this article, we will discuss how Python makes a directory with the help of examples.
In fact, the Python script can either make the directory permanent or just make it temporary, depending on which modules are imported.
The OS module is the Python module that is most often used to make a directory. With this module, the directory can be made permanent.
In addition, Python has a module called tempfile that can be used to make a temporary directory. This tutorial showed how to use different functions of the OS modules to make a directory in Python.
Creating a directory in Python
Python’s OS module has functions that let you interact with the operating system. OS is one of the standard utility modules in Python. This module gives a portable way to use functionality that depends on the operating system.
There are many ways to interact with the file system in the os and os.path
modules. All functions in the os module
throw an OSError
if a file name or path is wrong or can’t be reached, or if another argument has the right type but isn’t accepted by the operating system.
The dir() function gives back all of the object’s properties and methods, but not their values. This function will return all the properties and methods, even the ones that are built into the object and are always there.
There are several approaches for creating a director within the OS module. Listed below are…
- os.mkdir()
- os.makedirs()
os.mkdir() python
Python’s os.mkdir()
method is used to create a directory named path with the given mode. This method raises FileExistsError if the specified directory already exists.
Syntax:
os.mkdir(path, mode = 0o777, *, dir_fd = None)
Parameter:
- path: An object resembling a path that represents a file system path. A string or bytes object expressing a path constitutes a path-like object.
- mode(optional): A positive integer indicating the mode of the directory to be created. If this argument is missing, Oo777 is used as the default value.
- dir_fd (optional): A file descriptor that specifies a directory. This parameter’s default value is None. If the path supplied is absolute, dir_fd is disregarded.
Note: The asterisk (*) in the parameter list indicates that any subsequent arguments (in this case, “dir_fd”) are keyword-only parameters and must be supplied by name, not as positional parameters.
- Return Type: This method does not return any value.
Example
import os
# Directory
directory = "PythonForFree"
# Parent Directory path
parent_dir = "D:/Pycharm projects/"
# Path
path = os.path.join(parent_dir, directory)
os.mkdir(path)
print("Directory '% s' created" % directory)
directory = "PFF"
# Parent Directory path
parent_dir = "D:/Pycharm projects"
# mode
mode = 0o666
# Path
path = os.path.join(parent_dir, directory)
os.mkdir(path, mode)
print("Directory '% s' created" % directory)
Output
Directory 'PythonForFree' created Directory 'PFF' created
python os.makedirs()
Python’s os.makedirs()
method is used to build a directory recursively. Thus, if any intermediate-level directories are missing while creating a leaf directory, the os.makedirs()
method will construct them all.
For example, consider the following path:
D:/Pycharm projects/PythonForFree/Authors/Nym
Suppose we wish to make the directory ‘Nym’, but the Directories ‘PythonForFree’ and ‘Authors’ do not exist on the path. The os.makedirs()
method will then create all missing or inaccessible directories in the supplied path. The ‘PythonForFree’ and ‘Authors’ directories will be generated initially, followed by the ‘Nym’ directory.
Syntax:
os.makedirs(path, mode = 0o777, exist_ok = False)
Parameter:
- The first argument of this function is required and specifies the directory’s path.
- This function’s second optional argument is used to set the directory’s permissions for various users.
- The third argument is not required. If the specified directory already exists, OSError will be displayed.
- This function does not return anything.
Example
# Python program to explain os.makedirs() method
# importing os module
import os
# Leaf directory
directory = "Nym"
# Parent Directories
parent_dir = "E:/Pycharm projects/PythonForFree/Authors"
# Path
path = os.path.join(parent_dir, directory)
# Create the directory
# 'Nym'
os.makedirs(path)
print("Directory '% s' created" % directory)
# Directory 'PythonForFree' and 'Authors' will
# be created too
# if it does not exists
# Leaf directory
directory = "c"
# Parent Directories
parent_dir = "E:/Pycharm projects/PythonForFree/a/b"
# mode
mode = 0o666
path = os.path.join(parent_dir, directory)
# Create the directory 'c'
os.makedirs(path, mode)
print("Directory '% s' created" % directory)
# 'PythonForFree', 'a', and 'b'
# will also be created if
# it does not exists
# If any of the intermediate level
# directory is missing
# os.makedirs() method will
# create them
# os.makedirs() method can be
# used to create a directory tree
Output
Directory 'Nym' created Directory 'c' created
How do you create a directory in Python?
Python’s os. makedirs()
method is used to build a recursive directory. This means that if any intermediate directories are missing while creating a leaf directory, the os. makedirs(
) method will create them all.
As usual, there are numerous ways to accomplish this. If you wish to construct a single directory with the os module, you can call os.mkdir()
(make directory) with the directory name to create a single subdirectory with the specified name.
What is the Dir function in Python?
The dir() function returns all properties and methods without their associated values. This function will return all properties and methods, including those that are inherent to the object type.
Conclusion
The ways of creating permanent and temporary directories in Python have been shown in this tutorial by using the functions of OS modules.
- Use the
os.mkdir()
function to make a new directory. - Use
os.makedirs()
method to build a recursive directory.
I hope you’ve learned a lot from this. If you have any questions, please leave a comment below. For more Python tutorials, kindly visit our website.
Thank you!