How can I create a directory if it does not exist using Python?

PythonProgrammingServer Side Programming

Python has built in file creation, writing, and reading capabilities. In Python, there are two sorts of files that can be handled: text files and binary files (written in binary language, 0s, and 1s). While you can create files you may delete them when you no longer need them.

It is simple to create directories programmatically, but you must ensure that they do not already exist. You'll have difficulties if you don't.

Example 1

In Python, use the os.path.exists() method to see if a directory already exists, and then use the os.makedirs() method to create it.

The built in Python method os.path.exists() is used to determine whether or not the supplied path exists. The os.path.exists() method produces a boolean value that is either True or False depending on whether or not the route exists.

Python's OS module includes functions for creating and removing directories (folders), retrieving their contents, altering and identifying the current directory, and more. To interface with the underlying operating system, you must first import the os module.

#python program to check if a directory exists import os path = "directory" # Check whether the specified path exists or not isExist = os.path.exists(path) #printing if the path exists or not print(isExist)

Output

On executing the above program, the following output is generated.

True
Let’s look at a scenario where the directory doesn’t exist.

Example 2

The built in Python method os.makedirs() is used to recursively build a directory.

#python program to check if a directory exists import os path = "pythonprog" # Check whether the specified path exists or not isExist = os.path.exists(path) if not isExist: # Create a new directory because it does not exist os.makedirs(path) print("The new directory is created!")

Output

On executing the above program, the following output is generated.

The new directory is created!

Example 3

To create a directory, first check if it already exists using os.path.exists(directory). Then you can create it using −

#python program to check if a path exists #if it doesn’t exist we create one import os if not os.path.exists('my_folder'): os.makedirs('my_folder')

Example 4

The pathlib module contains classes that represent filesystem paths and provide semantics for various operating systems. Pure paths, which give purely computational operations without I/O, and concrete paths, which inherit from pure pathways but additionally provide I/O operations, are the two types of path classes.

# python program to check if a path exists #if path doesn’t exist we create a new path from pathlib import Path #creating a new directory called pythondirectory Path("/my/pythondirectory").mkdir(parents=True, exist_ok=True)

Example 5

# python program to check if a path exists #if path doesn’t exist we create a new path import os try: os.makedirs("pythondirectory") except FileExistsError: # directory already exists pass
raja
Updated on 16-Sep-2022 07:13:36

Advertisements