Python Create Directory: Complete How-to guide

In this article, we will see how to create a directory by using python. Python can help us communicate with the operating system, it can read and write files for us directly from the program.

To create a directory in python we will use a Python OS module that contains various in-built functions to deal with and interact with the underlying operating systems and the files.

You may require files to store data created by a program. And to store the files you may require a directory so that you can access them whenever needed. That’s where the Python OS module can help you.

Also read: How to convert list to string in python

There are basically two types of methods available in the OS module for creating a directory.

  • os.mkdir() : Creates single directory
  • os.makedirs() : Creates Multiple directories

Python create Directory

To create a single directory in the system we use os.mkdir() method.

os.mkdir(path, mode)

This method takes two paratmeters:

  • Path (Required): A file path at which you want to create a directory. It can either be a string or Byte value representing a path and name of the directory to be build
  • Mode (Optional): This method refers to the access or permission required to deal with the file operations in a directory. The default value is ‘0o777‘.

How to create a Directory using Python in the specified location

Python os.mkdir() Example

To create a single directory in a specified location follow the example below.

import os

 path = "/home/techstricks/funpython"
 os.mkdir(path)
 print("Directory '% s' is created!" % path")
Directory /home/techstricks/funpython is created!

Python os.mkdir() Permissions Example

We can use the optional method to provide permission to deal with readable and writeable operations within the directory.

import os 

 path_dir = "C:/TechSTricks"
 os.mkdir(path_dir,mode = 0o666) 
 print("Directory '% s' is built!" % path_dir) 

The mode setting of 666 allows read and write file operations within the created directory. You can also use mode 755 to give read access to all the users but writing access will only be with the owner. We need to specify 0o before our access parameter.

Directory 'C:/TechSTricks' is built!

Python os.mkdir() Function Exceptions

The os.mkdir() method will raise an FileExistsError Exception if the directory in the specified location already exists.

import os 
 path = "C:/TechSTricks"
 os.mkdir(path) 
 print("Directory '% s' is built!" % path) 
Traceback (most recent call last):   File "tst.py", line 18, in      os.mkdir(path) FileExistsError: [WinError 183] Cannot create a file when that file already exists: 'C:/TechSTricks'

How to create multiple directories using Python

os.makedirs() the method in Python is used to create a nested directory or recursive directory within the system.

While making a leaf directory if any intermediate-level directory is missing, this method will help to create them all.

os.makedirs(path,mode)

Suppose you want to create a directory named Twitch in an unnamed folder called Internet/Streaming. The os.makedirs() will create all the directories if they were not created.

import os 

 path_dir = "C:/Internet/Streaming/Twitch"
 os.makedirs(path_dir,mode = 0o666) 
 print("Directory '% s' is built!" % path_dir) 
Directory 'C:/Internet/Streaming/Twitch' is built!

Creating a directory in Python is simple and common practice. Most projects require directories to either store data or access data. Mastering this method can be helpful as a developer.

We will be happy to hear your thoughts

      Leave a reply

      Techs Tricks
      Logo
      Reset Password
      Shopping cart