How to create a Directory in python ?

In this very special i will learn to different APIs in python to create directories if not exists recursive also i learn to python create directory recursively.

Creating a Directory in Python

Python’s OS module list contains a method to create a directory Examples.
python get current directory

os.mkdir(path)

python create directory
python create directory

Also Read: convert string to array python

It creates a directory with given path i.e.

python create file

os.mkdir('temporaryDir')

It creates the directory ‘temporaryDir’ in current directory.

how to create folders and subfolders in python?
here If python folder already exists then it will raise here some FileExistsError Error. so to all the avoid some errors either i should call it using calling a try / except bellow Example if no such file or directory python create directory.

# Create directory
folderNm = 'temporaryDir'
try:
    # Create target main temp Directory
    os.mkdir(folderNm)
    print("Archive " , folderNm ,  " You have Created Directory Successfully ") 
except FileExistsError:
    print("Archive " , folderNm ,  " already exists")

Also Read: python unzip
onther way to i should first of all check if given already folder exists or not follows best example.

# Create a here simple target Directory if don't exist
if not os.path.exists(folderNm):
    os.mkdir(folderNm)
    print("Archive " , folderNm ,  " You have Created Directory Successfully ")
else:    
    print("Archive " , folderNm ,  " already exists")

Also Read: Linear Search in C, C++, JAVA, PHP, Python3 and C#

Read Also:  glob images - Php list all images in folder and subfolders

os.mkdir(path) will create the given directory only, but it will not create the in-between directory in the given path.

For example we want to create ‘temporary/temporaryDir2/sample’ in current working directory. But neither temporary or temporaryDir2 is present in current working directory. Hence it will throw error i.e. python change working directory

folderNm = 'temporaryDir2/temporary2/temporary'
os.mkdir(folderNm)

Output:

FileNotFoundError: [Errno 2] No such file or directory: 'temporaryDir2/temporary2/temporary'

Also Read: python developer resume Examples

os.mkdir(path) can not create in-between directories in the given path,if they are not present. It will throws error in such cases. For that we need another API.

Creating Intermediate Directories in Python

Python’s OS module provides an another function to create a directories i.e.

os.makedirs(path)

os.makedirs(name) will create the directory on given path, also if any in-between-level directory don’t exists then it will create that too.

Its just like mkdir -p command in linux.

Let’s create a directory with in-between directories i.e.

Also Read: how to skip a line in python | python skip lines starting with #

folderNm = 'temporaryDir2/temporary2/temporary'
# Create target directory & all in-between directories if don't exists
os.makedirs(folderNm)  

It will Make all the directory ‘temporary’ and all its parent directories if they don’t exists.

Read Also:  How to Use Time Sleep in Python Script?

If target directory already exists then it will throw error. So, either call it using try / except i.e.

# Make target directory & all in-between directories if don't exists
try:
    os.makedirs(folderNm)    
    print("Archive " , folderNm ,  " You have Created Successfully ")
except FileExistsError:
    print("Archive " , folderNm ,  " already exists")  

Also Read: Shell Script to Add Two Numbers using ommand line arguments

or before calling check if target directory already exists i.e.

# Make target directory & all in-between directories if don't exists
if not os.path.exists(folderNm):
    os.makedirs(folderNm)
    print("Archive " , folderNm ,  " You have Created Successfully ")
else:    
    print("Archive " , folderNm ,  " already exists")  

Also Read: how to split a string in python? | python split string into list

Full example is as follows,

import os
def main():
    
    # Make directory
    folderNm = 'temporaryDir'
    
    try:
        # Make target Directory
        os.mkdir(folderNm)
        print("Archive " , folderNm ,  " You have Created Successfully ") 
    except FileExistsError:
        print("Archive " , folderNm ,  " already exists")        
    
    # Make target Directory if don't exist
    if not os.path.exists(folderNm):
        os.mkdir(folderNm)
        print("Archive " , folderNm ,  " You have Created Successfully ")
    else:    
        print("Archive " , folderNm ,  " already exists")
    
    folderNm = 'temporaryDir2/temporary2/temporary'
    
    # Make target directory & all in-between directories if don't exists
    try:
        os.makedirs(folderNm)    
        print("Archive " , folderNm ,  " You have Created Successfully ")
    except FileExistsError:
        print("Archive " , folderNm ,  " already exists")  
        
    
    # Make target directory & all in-between directories if don't exists
    if not os.path.exists(folderNm):
        os.makedirs(folderNm)
        print("Archive " , folderNm ,  " You have Created Successfully ")
    else:    
        print("Archive " , folderNm ,  " already exists")    
        
    
if __name__ == '__main__':
    main()

Output:

Archive  temporaryDir  You have Created Successfully 
Archive  temporaryDir  already exists
Archive  temporaryDir2/temporary2/temporary  You have Created Successfully 
Archive  temporaryDir2/temporary2/temporary  already exists

Also Read: How to Use Time Sleep in Python Script?