Working with files is a common operation in Python. Files can be used to store data outside of a program that can be referenced by a program in the future. When you’re working with files, you may encounter a situation where you want to create a new directory in which a file should be stored.
That’s where the Python os module comes in. The os module includes two methods called
os.mkdirs() which can be used to create directories in Python.
This tutorial will explore how to create a directory in Python using the
os.mkdirs() methods. We will also use a few examples to show how these methods work.
Python os Refresher
Before you can work with files in Python, you need to import the os module. The os module is built-in to Python and contains a number of methods that can be used to interface with a computer’s underlying operating system. In this case, we are interested in the os module’s file system methods, which can be used to work with files.
We can use an
import statement to import os into our program. Here’s the code we would use to accomplish this task:
Now that we’ve imported os in our program, we are ready to start creating directories in Python.
Python Create Directory
os.mkdir() method can be used to create a single directory. For example, if we were creating a program that needs a new folder to store its outputs, we would use
The syntax for the
os.mkdir() method is as follows:
os.mkdir() function takes in two parameters:
- path is the file path at which you want to create your directory (required)
- access refers to the access rights you want to grant your directory (optional)
Let’s explore an example to showcase how to use the
Say that we are creating a program that analyzes a list of math student test scores and stores whether or not each student has passed the latest test. We first want to create a directory where our data will be stored, so that our data will be separate from the rest of our program.
Here’s the code we could use to create a directory in which our data can be stored:
import os path = "/home/school/math/grades/final" os.mkdir(path) print("/home/school/math/grades/final has been created.")
Our code creates a new directory called
final in the
/home/school/math/grades folder in our operating system. Then, it returns the message:
/home/school/math/grades/final has been created.
mkdir() method can only be used to create one directory at a time. So, if we wanted to create a folder called
final and then create a folder within
jan2020, we could not use one
In addition, we can use the optional
access parameter to specify the permissions we want our directory to have. By default, the access permissions for the directory
mkdir() creates is 777, which means the directory is readable and writable by the owner and all other users. But what if we want to set our own permissions?
Say that our operating system is accessible to multiple teachers, and we only want to grant write access to the owner. In order to do so, we would need to use the 755 permission, which states that a file is readable and accessible by all users, but only the owner can write to the file.
Here’s the code we could use to create a working directory with custom permissions:
import os path = "/home/school/math/grades/final" access = 0o755 os.mkdir(path, access) print("/home/school/math/grades/final has been created.")
In the above program, we have created a target directory called
/home/school/math/grades/final which has the access permission 755. This means that our file can be read by all users, but only the owner can write to the file. Then, our program prints the message:
/home/school/math/grades/final has been created.
It’s important to note that our
access variable is equal to
0o755 in our code. We have done this because access rights use the octal prefix, and so we need to specify
0o before our access parameter.
If you’re looking to learn about how to check if a file or directory already exists, check out our tutorial on checking if files exist in Python.
FREE Python Fundamentals Workshop
Use the calendar below to reserve your seat.
Demand for people who know Python is soaring! In this free online workshop, learn the fundamentals of Python and meet other Career Karma members who are building with Python.
Python Create Multiple Directories
mkdir() method can only be used to create one directory. However, there are often cases where we need to create directories that exist within other new directories.
For instance, say we are creating a program that stores our math student test score data. Instead of storing all of our data in one folder, we could create a more complex file structure that stores data in folders depending on the year and month of the test. Doing so would make our data easier to find when we want to refer back to it.
That’s where the
os.makedirs() method comes in.
os.makedirs() takes in the same parameters as
os.mkdir() but is capable of creating subdirectories within a new directory.
Let’s use our school math test example to illustrate how the
os.makedirs() method works. As we mentioned above, the specifications of our program have changed, and now we want to store data in folders depending on the year and month of the test. We could accomplish this task using the following code:
import os path = "/home/school/math/grades/2020/02" access = 0o755 os.mkdir(path, access) print("/home/school/math/grades/2020/02 has been created.")
Let’s say that we do not yet have a
2020 folder or an
02 folder. This code would first create the
2020 folder, then it would create the folder called
2020. In our code above, we assign this folder the access permissions 755, which means that anyone can read the directory, but only the owner can write to the folder.
Then, after our program creates our folder, it prints out the message:
/home/school/math/grades/2020/02 has been created.
Creating a directory is a common operation in Python when you’re working with files. The
os.mkdir() method can be used to create a single directory, and the
os.makedirs() method can be used to create multi-level directories.
This tutorial discussed how to use both
os.makedirs() to create folders in Python. We also explored an example of each of these methods in action and analyzed how the access parameter can be used to set permissions for the files our program creates.
Now you’re ready to start creating directories using the os module like an expert!