How to Create a New Directory in Python



Python


In this article, we show how to create a new directory in Python.

So, say, a directory does not exist on your computer's operating system and you want to create a new directory (folder). Python allows us to do this with its built-in function, mkdir().

All we have to do is use the mkdir() function and inside the parameter of this function specify the directory we want to create. If you are creating a directory in another directory other than the current working directory, then you have to specify the full path. We will show both scenarios in this article

To create a directory in the current working directory, the code to do is shown below.



First, in our code, we import the os module.

After this, we create the directory, PythonProjects.

This code creates the directory, PythonProjects, in the current working directory.

If you don't specify a path to the directory, it will create the directory in the current working directory.

We go over below how to create a directory in a directory other than the current working directory.

To create a new directory anywhere on the operating system, we have to specify the full path to that directory.

The code below shows how to do this.



So, the code above now creates the directory, PythonProjects in the C:\\Users\\David directory.

So this is how we can create new directories with Python.


Related Resources



HTML Comment Box is loading comments...