How to create a directory in Python
In this tutorial, we are going to see how to create a new directory in Python.
Let’s say, there is no directory on your operating system and you want to create a new directory (or folder). Python has a built-in function called mkdir().
So, we are going to use the mkdir() function and in the parameter of that function specify the directory we want to create. If you want to create a directory outside the current directory, you must specify the full path. We will see both scenarios in this tutorial
To create a directory in the current directory, the code is shown below.
import os os.mkdir('myDirectory')
First, our code imports the os module. After that, we create the directory “myDirectory”.
To create a new directory anywhere on the operating system, we need to specify the full path to this directory.
The code below shows how to do this.
import os os.mkdir('C:\\Users\\StackHowTo\\MyDirectory')
The above code creates “MyDirectory” in “C:\Users\StackHowTo\MyDirectory”.