How to check if directory exists in Python

Home /

Table of Contents

About Python Directory

In Python, a directory is a folder that contains files and other directories. Directories are used to organize and manage files on a computer system. In Python, you can work with directories using the ‘os‘ module, which provides a variety of functions for manipulating files and directories.

You can create a new directory using the ‘os.mkdir()‘ function, remove a directory using the ‘os.rmdir()‘ function, and get a list of files and directories in a directory using the ‘os.listdir()‘ function.

Here’s an example of creating a new directory using Python:

Python
import os

dir_name = "new_directory"
os.mkdir(dir_name)

In this example, the ‘os.mkdir()‘ function is used to create a new directory called “new_directory” in the current working directory. You can replace “new_directory” with the name of the directory you want to create, and specify a different path to create the directory in a different location.

Check if directory exists in Python

You can use the ‘os.path.isdir()‘ function to check if a directory exists in Python. This function takes a path as an argument and returns ‘True‘ if the path exists and is a directory, and ‘False‘ otherwise.

Here’s an example:

Python
import os

dir_path = "/path/to/directory"

if os.path.isdir(dir_path):
    print(f"The directory {dir_path} exists!")
else:
    print(f"The directory {dir_path} does not exist.")

In this example, replace ‘/path/to/directory‘ with the actual path of the directory you want to check. If the directory exists, the program will print “The directory ‘/path/to/directory‘ exists!”, otherwise it will print “The directory ‘/path/to/directory‘ does not exist.”.

Merge two directory

To merge two directories in Python, you can use the ‘shutil‘ module. The ‘shutil‘ module provides a number of high-level operations on files and collections of files, including functions for copying and moving files and directories.

Here’s an example of merging two directories using Python:

Python
import shutil

src_dir = "/path/to/source_directory"
dst_dir = "/path/to/destination_directory"

shutil.copytree(src_dir, dst_dir)

In this example, ‘src_dir‘ is the path to the source directory you want to merge, and ‘dst_dir‘ is the path to the destination directory where you want to merge the source directory. The ‘shutil.copytree()‘ function is used to copy the source directory to the destination directory, effectively merging the two directories.

Note that if the destination directory already exists, the ‘shutil.copytree()‘ function will raise an error. To avoid this, you can check if the destination directory exists using the ‘os.path.exists()‘ function before calling ‘shutil.copytree()‘, or use a different function such as ‘shutil.copy()‘ or ‘shutil.move()‘ to copy or move individual files from the source directory to the destination directory.

Share The Tutorial With Your Friends
Twiter
Facebook
LinkedIn
Email
WhatsApp
Skype
Reddit
Other Recommended Article