Creating a folder in python

file_system_create_folder

Skill - Creating a folder in python

Table of Contents

Skills Required

Please make sure to have all the skills mentioned above to understand and execute the code mentioned below. Go through the above skills if necessary for reference or revision


In this post, we will learn to create a folder using mkdir and makedirs function

Create a folder using ‘mkdir’ function

# import the os module in order to use mkdir function
import os

# specify the file we desire to check
fPath = r'C:\Users\Nagasudhir\Documents\Python Projects'

try:  
    # use mkdir function to create the folder
    os.mkdir(fPath)  
except Exception as e:  
    print(e)

We used try except to catch exception which can occur if the folder we are trying to create already exists

Create a folder with intermediate folders using ‘makedirs’ function

If we want to create intermediate folders also while creating a folder, use makedirs function

import os

# specify the file we desire to check
fPath = r'C:\Users\Nagasudhir\Documents\Python Projects'

# use makedirs function to create the folder along
# with intermediate folders if required
os.makedirs(fPath, exist_ok=True)

by using exist_ok=True, exception will not be thrown if folder already exists

Video

The video for this post can be seen here

Online Interpreter

Although we recommend to practice the above examples in Visual Studio Code, you can run these examples online at https://www.tutorialspoint.com/execute_python_online.php

References


Table of Contents

Comments