How to Check if a File or Directory Exists in Python

By | March 1, 2020

While writing a program you may require the information related to the files and directories present in the memory, maybe you want to make sure that a data file is present in the memory, or maybe you want to prevent the file overwriting. Using the Python programming we can create new directories in the memory and for some reason, we may want to know that the directory which we are supposed to create is already present in the memory or not.

How to Check if a File or Directory Exists in Python

In python, we have several methods to check whether a file or directory exists in the memory or not.

Here we have provided three major techniques to check whether the file or directory already in the memory or not?

  • Using OS Module
  • Using File Handling technique
  • Using pathlib module

Using OS Module:

All the creation of file and directories in a system is managed by its operating system and python os module help us to interact with the system operating system.

in os module we have 3 methods to check for the file and directory existence:

  • isfile()
  • isdir()
  • exists()

isfile()

The isfile() method accept a path as an argument and return the result in a boolean value if the path given to the method consists a file then it will return True else it returns False

Example:

from os import path

if path.isfile("PASS.txt"):    #this will check for the PASS.txt file in the same folder where the program source file is present.
    print("File Found")
else:
    print("File not found")
Output:
File Found

isdir()

isdir() method is used to check whether the directory is present in the memory or not. Like the isfile() method isdir() takes an argument and return a boolean value True or False.

Example:

from os import path

#here in path we have used double backslash (//) the fist backslash is for character escape and second backslash is for the directory separator.
if path.isdir("C:\\Users\\Public"):
    print("This path is correct and we have this directory")
else:
    print("Directory not found")

Output:

This path is correct and we have this directory

exits():

The exists method is used to check whether the specified path exists or not. With the help of this method, we can check for both existence directory as well as file.

Example:

from os import path
if path.isdir("C:\\Users\\Public"):
    print("This path is Exist")
else:
    print("There is no such path")
Output:
This path is Exist

File Handling

With the help of file handling, we can check for the existence of a file in the memory. This is one of the simplest ways to check for the file existence and it even does not require any modules.

Example:
try:
    with open("C:\\Users\\Public\\Downloads"):
        print("File Exist")
except:
    print("File Does not exist")
Output:
File Does not exist

Using pathlib module

Only python 3.4 and above versions have pathlib module and this is used to interact with the system path for file management. It provides an object-oriented interface and abstraction to interact with the system path.

Similar to the os module the pathlib module has a method Path that can be used to check the existence of a file or directory.

Example:
>>> import pathlib
>>> path = pathlib.Path(Pass.txt')
>>> path.exists()
True
>>> path.is_file()                              #to check whether it is a file or not
True

Conclusion

For most of the cases when we deal with the Operating system or file management we use the python os module, the pathlib module is also similar. The file handling technique is not that efficient because it can only be used to check the file existence in the directory.

You may be also interested in:

Leave a Reply

Your email address will not be published. Required fields are marked *