How to Create a Folder Using Python?


Have you ever wanted to automate the process of creating a folder? Well, using Python, you can! In this blog post, we’ll show you how to create a folder using the os module in Python.

Importing the os Module

The first thing we’ll need to do is import the os module. The os module allows us to interact with the operating system. We can use the os module to get a list of files in a directory, create new directories, and much more. To import the os module, we can use the following code:

import os
Code language: JavaScript (javascript)

Creating a New Directory

Now that we’ve imported the os module, we can use it to create a new directory. To do this, we’ll use the mkdir() method. The mkdir() method takes two arguments: the path of the new directory and an optional integer value representing various permissions on the new directory.

For example, if we want to create a new directory called “New_Folder” in our current working directory, we can use the following code:

os.mkdir("New_Folder")
Code language: JavaScript (javascript)

You can also create multiple folders using python. The syntax for creating multiple folders using python is as follows:

import os root_path = 'C:\path' folders = ['folder 01', 'folder 02', 'folder 03'] for folder in folders: os.mkdir(os.path.join(root_path, folder))
Code language: Python (python)

You can also create a folder with subfolders using python. The syntax for creating a folder with subfolders using python is as follows:

import os os.makedirs('folder/subfolder/') # to ensure that the string ended with a '/' (or '\', on Windows)
Code language: Python (python)

Conclusion:

In this blog post, we showed you how to create a folder using Python’s os module. We hope you found this post helpful! If you have

Andy Avery

I really enjoy helping people with their tech problems to make life easier, ​and that’s what I’ve been doing professionally for the past decade.

Recent Posts