Python provides the mkdir()
method in order to create directories in different operating systems like Linux, Windows, etc. The mkdir() method is provided via the os
module. The mkdir() method creates the specified directory with its only name or using the full path. If the directory already exists the creation returns an error.
os.mkdir() Syntax
The syntax of the os.mkdir() method is like below.
os.mkdir(DIRECTORY,mode=MODE)
- DIRECTORY is the directory name we want to create. The DIRECTORY can be also the full path of the directory with its name.
- MODE is the permission information like “777”,”700″ etc.
Create Directory
We can create a directory with the os.mkdir() method. In the following example, we create a directory named pythontect
in the current working directory.
import os
os.mkdir("pythontect")
During the usage of the mkdir() method we can provide the directory full path with the directory name like below. In the following example, we create the directory named “pythontect” in the “/home/ismail/” for Linux operating system.
import os
os.mkdir("/home/ismail/pythontect")
For the Windows operating systems, we can use the following example to create a directory with a full path using mkdir() method.
import os
os.mkdir("C:\Users\ismail\pythontect")