Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages

How to Create a Directory in Python

Ways To Create A Directory In Python

Hello, folks! In this article, we will be unveiling different ways to Create a Directory in Python


Steps to Create a Directory in Python

Python os module contains various in-built functions to deal with and interact with the underlying operating systems and the files.

The os module contains various in-built functions create directories in the system.

In the upcoming sections, we will have look at the various ways through which you can create a directory using the os module.


Technique 1: Using os.mkdir() method to Create a Directory in Python

The os module has in-built os.mkdir() method to create a directory in the system.

Syntax:

os.mkdir(path, mode)
  • path: The location wherein the user wants the directory to be created. It is a string or byte value which includes the entire path and name of the directory to be built.
  • mode: The permissions that needs to be given to deal with the file operations within the directory. The default value being ‘0o777‘.

Example 1: Create a Directory using Python in the specified location.

 
import os 

main_dir = "C:/Practice"

os.mkdir(main_dir) 
print("Directory '% s' is built!" % main_dir) 

Output:

Directory 'C:/Practice' is built!
Creating a directory-2 using mkdir()
Creating a directory-2 using mkdir()

Example 2: Providing permissions to deal with read and write operations within the directory.

 
import os 

main_dir = "C:/JournalDev"

os.mkdir(main_dir,mode = 0o666) 
print("Directory '% s' is built!" % main_dir) 


Setting mode = 0o666, allows read and write file operations within the created directory.

Output:

Directory 'C:/JournalDev' is built!
Creating a directory-1 using mkdir()
Creating a directory-1 using mkdir()

Exceptions with os.mkdir() function

The os.mkdir() method raises a FileExistsError Exception if the directory in the location specified already exists.

Example:

import os 

main_dir = "C:/JournalDev"

os.mkdir(main_dir,mode = 0o666) 
print("Directory '% s' is built!" % main_dir) 


Output:

FileExistsError                           Traceback (most recent call last)
<ipython-input-17-75731447cf21> in <module>
      3 main_dir = "C:/JournalDev"
      4 
----> 5 os.mkdir(main_dir,mode = 0o666)
      6 print("Directory '% s' is built!" % main_dir)
      7 

FileExistsError: [WinError 183] Cannot create a file when that file already exists: 'C:/JournalDev'

Technique 2: Using os.makedirs() method to create directory in the system

The os module has in-built os.makedirs() method to create nested or recursive directories within the system.

That is, the os.makedirs() function creates the parent directory, the intermediate directories as well as the leaf directory if any of them is not present in the system files.

Syntax:

os.makedirs(path,mode)

Example:

import os 
main_dir = "C:/Examples/Python_files/OS_module"

os.makedirs(main_dir,mode = 0o666) 
print("Directory '% s' is built!" % main_dir) 

In the above example, the makedirs() function creates the intermediate directories – ‘Python_files’ as well as the leaf directory – ‘OS_module’ in one shot through the function.

Output:

Directory 'C:/Examples/Python_files/OS_module' is built!
Creating a parent directory using makedirs()
Creating a parent directory using makedirs()
Creating an Intermediate Directory Using makedirs()
Creating an Intermediate Directory Using makedirs()
Creating a Base Directory using makedirs()
Creating a Base Directory using makedirs()

Conclusion

Thus, in this article, we have understood the ways to create directories within the system using the os module.


References