Create Directory if it does not exists

A python program to check if directory with given name exists, and create if it does not. First thing that we need to do is import module called “os” and use the function inside it to check if the directory exists or not.


__author__ = 'sureshkumarmukhiya'

import os

def makedir(directory):
    if not os.path.exists(directory):
        os.makedirs(directory)
        print ("New Directory Created with Name, " + directory)
    else :
        print (directory + " directory Already Exists!")


makedir('images')

If you run this program for the first time, and suppose the images directory does not exists in your current working directory, it will create images folder. And if you run this program again, it will exit with message, “images directory Already Exists!”

About the Author

dr.code.skm

I am backend developer with passion in Web Application development using latest technologies like Laravel, PHP7, React, ECMAScript 6 and WordPress. I prefer spending time analyzing big data with Apache Spark. Apart from that, I do photography.