How to create a new directory with Python

Being rich in features, Python provides all kinds of utilities required to interact with the functionalities offered by the operating system. The Python coder can easily access and unlock the functionalities offered by his or her operating system through the usage of the builtin os module.

Through this tutorial, you are going to learn how to make use of the Python builtin os module to create a new directory in your own operating system. Based on my experience with the command which I am going to share through this tutorial, it works both on Windows and Unix based machines.

Before going any further, make sure to launch a new interactive Python shell in your own operating system, so you can practice the utility being shared through this article, by yourself.

What's the one thing every developer wants? More screens! Enhance your coding experience with an external monitor to increase screen real estate.

Once you have launched the Python interactive shell in your own operating system, make sure to import the utility required for the purpose of this tutorial, by following the command shown in the example below.

from os import mkdir

Then declare the path of the directory which you want to create by making use of the syntax shown below.

new_directory_path = ‘/Users/oltjano/Desktop/Test’ # your path here

Once you have finished declaring the path for the new directory which you want to create, make use of the following command to accomplish the task.

mkdir(new_directory_path)

Final thoughts

As you learned through this tutorial, one can easily make use of the os.mkdir Python builtin utility to create a new directory in their own operating system.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download

Leave a Reply

Your email address will not be published. Required fields are marked *

*