All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

Create Directory In Python

Last Updated : Mar 11, 2024

Create Directory In Python

In this article we will show you the solution of create directory in python, Python allows you to create directories (also called folders) according to your needs. As part of this course, you will also learn how to create nested directories.

Working with directories in Python requires you to include the os module in your project, which allows you to interact with your operating system.

Several applications were built with Python, some of which required a directory to be created before they could run. We will now discuss the idea of how to add python to path in python with an example.

Step By Step Guide On Create Directory In Python :-

import os
def main():
    # Create directory
    dirName = 'tempDir'
    try:
        # Create target Directory
        os.mkdir(dirName)
        print("Directory " , dirname , " created ")
    except FileExistsError:
        print("Directory " , dirname , " already available")
    # Create target Directory if don't exist
    if not os.path.exists(dirName):
        os.mkdir(dirName)
        print("Directory " , dirname , " created ")
    else:
        print("Directory " , dirname , " already available")
    dirName = 'tempDir2/temp2/temp'
    # Create target directory & all intermediate directories if don't exists
    try:
        os.makedirs(dirName)
        print("Directory " , dirname , " created ")
    except FileExistsError:
        print("Directory " , dirname , " already available")
    # Create target directory & all intermediate directories if don't exists
    if not os.path.exists(dirName):
        os.makedirs(dirName)
        print("Directory " , dirname , " created ")
    else:
        print("Directory " , dirname , " already available")
if __name__ == '__main__':
    main()
  1. Import the built-in Python module "os" which provides a way to interact with the operating system.
  2. Define a function named "main()" which will be called when the script is run.
  3. Assign the directory name 'tempDir' to a variable named "dirName".
  4. Try to create the directory using the command "os.mkdir(dirName)".
  5. If the directory already exists, catch the resulting "FileExistsError" exception and print a message saying that the directory is already available.
  6. Use the "os.path.exists()" function to check if the directory exists.
  7. If the directory doesn't exist, create it using the command "os.mkdir(dirName)".
  8. If the directory already exists, print a message saying that the directory is already available.
  9. Change the directory name to 'tempDir2/temp2/temp' and assign it to the variable "dirName".
  10. Try to create the directory and all intermediate directories using the command "os.makedirs(dirName)".
  11. If any of the intermediate directories already exist, catch the resulting "FileExistsError" exception and print a message saying that the directory is already available.
  12. Use the "os.path.exists()" function to check if the directory exists.
  13. If the directory doesn't exist, create it and all intermediate directories using the command "os.makedirs(dirName)".
  14. If the directory already exists, print a message saying that the directory is already available.
  15. Call the "main()" function if the script is run directly (i.e. not imported as a module).

Conclusion :-

As a result, we have successfully learned how to add python to path in python with an example.

Here we have demonstrated how to create a directory by using the mkdir() method, which will create it if all parent directories on the path are present, otherwise we can use the makedirs() method to create all intermediate-level directories automatically.

Both methods can be used as part of an OS module in Python.

I hope this article on create directory in python helps you and the steps and method mentioned above are easy to follow and implement.