All TalkersCode Topics

Follow TalkersCode On Social Media

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

Python Check If Directory Exists

Last Updated : Mar 11, 2024

Python Check If Directory Exists

In this article we will show you the solution of python check if directory exists, the Python interpreter raises an Exception when the file or directory corresponding to the given path does not exist. It is important that you address this issue, otherwise your code will crash.

In this article, we discuss different methods for determining if a file or directory exists in Python, along with the steps to open a file safely.

Checking whether a directory or file exists in Python can become important sometimes, because you may be wanting to avoid overwriting an existing file, or maybe you want to check whether the file is available or not before loading it.

We will now discuss the idea of how to check if a directory exists in python with an example.

Step By Step Guide On Python Check If Directory Exists :-

# importing os.path module
import os.path
# Create a directory
# (in current working directory)
dirname = "TalkersCode"
os.mkdir(dirname)
# Create a symbolic link
# pointing to above directory
symlink_path = "/home/User/Desktop/tc"
os.symlink(dirname, symlink_path)
path = dirname
# Now, Check whether the
# specified path is an
# existing directory or not
isdir = os.path.isdir(path)
print(isdir)
path = symlink_path
# Check whether the
# specified path (which is a
# symbolic link ) is an
# existing directory or not
isdir = os.path.isdir(path)
print(isdir)
  1. In the first step, we import the os.path module that provides methods for handling file and directory paths.
  2. As part of the process of creating 'TalkersCode', we utilize the os.mkdir() function. A directory named this is created in the directory where the current working session is running.
  3. Through the use of the os.symlink() function, we create a symbolic link to the previously created directory. As the first argument, we specify the path to the directory for which the symbolic link is to be created, and as the second argument, the path to the file or directory to be linked.
  4. Our next step is to set the 'path' variable to the name of the directory we created in this step.
  5. Checking whether the specified path is a directory with the os.path.isdir() function is the first step. In the event that the path exists and is a directory, this function will return 'True'; otherwise, it will return 'False'.
  6. A new window will then appear as soon as 'isdir' is printed to the console, indicating whether or not the path contains an existing directory. A value of 'True' will be assigned to the 'isdir' variable in this case because 'TalkersCode' exists.
  7. In this step, we created a symbolic link and then set the 'path' variable to that name.
  8. As before, we use the os.path.isdir() function to confirm whether the path leads to an existing directory. As a result, the symbolic link points to an existing directory, 'TalkersCode', so the 'isdir' variable will be 'True'.

Conclusion :-

As a result, we have successfully learned how to check if a directory exists in python with an example.

As you learned in this tutorial, Python can be used to check the existence of files and directories.

You learned how to accomplish this with an object-oriented approach using the pathlib library.

In the next lesson, you learned how to use the os library to determine whether a file or directory exists.

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

Author Image About Anjali

Experienced Computer Programmer with a broad range of experience in technology. Strengths in application development and Object Oriented architecture design, front end programming, usability and multimedia technology. Expert in coding languages such as C, C++ Java, JavaScript, PHP and more.

Follow Anjali On Linkedin 🡪