Member-only story
Whether you’re a seasoned developer or just starting your coding journey, mastering the art of creating and removing directories is a crucial skill. Python offers a user-friendly way to handle these tasks, making it easier for you to organize your projects and keep your file structure tidy.
In this article, we’ll walk through the simple steps to create, remove, and navigate directories using Python.
Creating a New Directory
Creating a new directory in Python is a breeze with the os
module. Here's how you do it:
import os
# Creating a new directory
os.mkdir("new_directory")
The os.mkdir()
function creates a new directory with the specified name in the current working directory. If you want to create a nested directory, you can use the os.makedirs()
function instead:
import os
# Creating a nested directory
os.makedirs("parent_directory/child_directory")
This will create both the parent_directory
and the child_directory
within it, if they don't already exist.