In this post, we are going to explore How to Create Directory single or nested in Python. We are going to use the built-in method in the OS module
Python OS module
The python os module provides some handy ways of using operating system dependent functionality. For manipulation, you can use the os. path module, for temporary directories and files, uses the tempfile module. If you need to read and write a file then use the open() method.
2 Ways to Create Directory in Python
In Python OS module has two built-in methods to create directories.
- os.mkdir() :create a new single directory
- os.makesdirs() :Create nested directory
How to get the current directory in Python?
To get the current directory we can use getcwd() this method will return a string containing the path of the current working directory.
import os
#get path of current directory
curr_dir = os.getcwd()
print (f"current working is {curr_dir}")
Output
current working is \admin\PyProgram
1. Create directory Using os.mkdir() method
Python OS module built-in function os.mkdir() is used to create a new single directory at a given path with a specified numeric mode.If the directory already exists at a given path then it will raise FileExistsError Error
Syntax
os.mkdir(path,mode)
- path :It is file system path where we want to create directory.It can be string or byte.
- mode : This is optional parameter that indicates the permission given to file inside the directory default value is Oo777.
2. Create directory/folder in Python
- First, import the os module using import os.
- Create a new directory using the mkdir() method.
- using try/catch block to handle the FileExistsError Error if the directory does not exist.
#program to create directory/folder in Python
import os
mydir = 'PersonalDoc'
try:
os.mkdir(mydir)
print(f'{mydir} is created')
except FileExistsError:
print(f"Directory {mydir} already exists")
Output
PersonalDoc is created
3. Create a directory, if it does not exists in Python
Steps to create a directory, if it does not exist in Python
- Instead of using the try/catch block, First, we can check if the directory does not exist using os. path.exists(mydir) method and then create a new directory using os. mkdir(mydir) method.
- else prints message directory already exists.
import os
mydir = 'PersonalDoc'
if not os.path.exists(mydir):
os.mkdir(mydir)
print(f'{mydir} is created')
else:
print(f"Directory {mydir} already exists")
Output
Directory PersonalDoc already exists
Important Note :
The os.mkdir() method does not create a nested directory or recursive directory so for that, we need to use the os.makesdirs() method.
3. Create nested directory Using os.makedirs()
os.makedirs() method creates a nested or recursive directory at the given path. os.makedirs() create parent folder at the given path, if sub-folder (intermediate) and leaf folder does not exist then it creates them as well.
Syntax
os.makedirs(path,mode)
4. Create nested directory in Python
In this example, os.makedirs() function creates the parent directory data and sub-directory Programs and leaf directory Python. So whenever need to create a nested directory we use makedirs(mydir) method.
If the directory already exists at the given path then it will raise FileExistsError Error so we are handling it with try/catch block.
#program to create folder/subfolder in Python
import os
mydir = 'data/Programs/Python/'
try :
os.makedirs(mydir)
print(f'{mydir} is created')
except FileExistsError:
print(f"Directory {mydir} already exists")
Output
data/Programs/Python/ is created
5. Create nested directory if does not exits
Steps to create nested directory if it does not exists.
- Instead of using the try/catch block, First, we can check if the directory does not exist using os. path.exists(mydir) method and then create a new nested directory by using os.makedirs(mydir) method.
- else prints message directory already exists.
#program to create folder/subfolder in Python if does not exists
import os
mydir = 'data/Programs/Python/'
if not os.path.exists(mydir):
os.makedirs(mydir)
print(f'{mydir} is created')
else:
print(f"Directory {mydir} already exists")
output
Directory data/Programs/Python/ already exists
Summary
In this post, we have explored different ways to create a directory in Python using os module methods
- mkdir() :create a new directory,a new nested directory.
- makedirs(): method to create a nested directory.
- if the directory does not exist then by using os. path.exists(mydir) method