Kodeclik Blog
Python os.mkdir()
The os.mkdir() function in Python is used to programmatically create a new directory, i.e., to create directories from within your Python program rather than manually create it.
os.mkdir() takes three arguments of which the first is required which is the actual path of the new directory to be created. The second, optional, argument is the path mode which specifies the access permission bits for the new directory. The third, also optional, argument is meant to denote the file descriptor of the parent directory where the new directory is to be created.
Python create directory with os.mkdir()
Here is the simplest possible usecase with os.mkdir():
import os
dirname = "Kodeclik"
os.mkdir(dirname)
In the above code, we first import the os module and then use the os.mkdir() function to create a directory called “Kodeclik”.
Before the program is run, the directory structure looks like:
In other words, other than the main file (main.py) containing this Python program, there are no other files or directories.
After the program is run, we see:
As we notice there is a new directory called “Kodeclik” created.
Python create a directory with same name as existing directory
If we try running this program again, we get this error:
Traceback (most recent call last):
File "main.py", line 5, in <module>
os.mkdir(dirname)
FileExistsError: [Errno 17] File exists: 'Kodeclik'
As is expected, we see that we cannot create another directory with the same name as an existing directory.
Python os.mkdir() to create multiple directories
We can use os.mkdir() to create multiple directories, like shown below:
import os
directories = ["Harry Potter", "Humpty Dumpty",
"Mickey Mouse","Donald Duck"]
for name in directories:
os.mkdir(name)
Before running the program, the directory structure looks like (note that we removed the Kodeclik directory which we had created earlier):
After running the program, we see:
Creating a full directory path in Python with all intermediate directories
Let us try to use os.mkdir() to create a complete directory path, i.e., a sequence of directories:
import os
os.mkdir('Kodeclik/Classes/Python')
We will get:
Traceback (most recent call last):
File "main.py", line 3, in <module>
os.mkdir('Kodeclik/Classes/Python')
FileNotFoundError: [Errno 2] No such file or directory:
'Kodeclik/Classes/Python'
Hmm - what happened? os.mkdir() does not work to create all intermediate level directories. For this purpose, we should use os.makedirs() like below:
import os
os.makedirs('Kodeclik/Classes/Python')
If we run this program we get:
In this blogpost, we have seen how Python’s os.mkdir() function can help create new directories in your project without manually creating them. We have seen the most basic usage scenarios for os.mkdir() but note that we have two other (optional) arguments for os.mkdir() but getting into these details is beyond the scope of this article. If you liked this, learn about Python's os.listdir() function that as the name indicates helps list the contents of a directory.
For more Python content, checkout the math.ceil() and math.floor() functions! Also
learn about the math domain error in Python and how to fix it!
Interested in more things Python? Checkout our post on Python queues. Also see our blogpost on Python's enumerate() capability. Also if you like Python+math content, see our blogpost on Magic Squares. Finally, master the Python print function!
Want to learn Python with us? Sign up for 1:1 or small group classes.