BLOG

Python Check if File Exists: How do I Search for File/Directory?

python check if file exists

It doesn’t matter if you want to avoid overwriting the existing file or check if the data file is available before loading, Python check if file exists, is a crucial check for every Python programmer/program. With Python, there are multiple ways to verify if a directory or file exists through functions designed with the standard library and core language. With this article, we are sharing a different way for checking if the file exists in Python.

Check If File Exists Using The os.path Module

Python exists () is a well-designed method that’s used for checking if the directory or file exists in the program or not. It also determines if the path denotes the file descriptor (open). With this method, if the file exists, the Boolean value is returned true; if the file doesn’t exist, the Boolean value is returned false. This method works in integration with os.path sub-module as the os.path.exists and os module. In the section below, we are sharing information about various ways through various functions, such as;

os.path.exists()

With this function, programmers can check if the directory or file exists. To utilize this function, follow the below-mentioned steps, such as;

  • First of all, import the os.path module from the os import path
  • Then, use path.exists() to check the availability or existence of the files i.e., path.exists(“guru99.txt”). The entire code is as;

import os.path

from os import path

def main():

print (“File exists:”+str(path.exists(‘guru99.txt’)))

print (“File exists:” + str(path.exists(‘career.guru99.txt’)))

print (“directory exists:” + str(path.exists(‘myDirectory’)))

if __name__== “__main__”:

main()

As the output of this command depicts, the file and directory options will show if it’s true or false.

os.path.isfile()

This function is conducted through Python isfile() which determine if the path exists in the regular file. If the path is available in the existing file, the Boolean value will be returned true; otherwise, it will be returned false. For checking if the input is the file or not, you can use isfile command, such as;

import os.path

from os import path

def main():

                print (“Is it File?” + str(path.isfile(‘guru99.txt’)))

                print (“Is it File?” + str(path.isfile(‘myDirectory’)))

if __name__== “__main__”:

                main()

In the output, the questions like “is it file?” will appear with true or false in front of it.

os.path.isdir()

For programmers who need to confirm if the path is pointing to the directory, os.path.isdir() can be used. The command is as;

import os.path

from os import path

def main():

   print (“Is it Directory?” + str(path.isdir(‘guru99.txt’)))

   print (“Is it Directory?” + str(path.isdir(‘myDirectory’)))

if __name__== “__main__”:

   main()

As for the output, the question will appear as, “is it directory?” and it will show results by adding true or false to it.

Check If File Exists Using The pathlib Module

pathlibPath.exists()

Python 3.4 and higher versions are designed with pathlib module for maintaining the file system path. This module utilized the object-focused approach to check if the file is available (or exists) or not.

import pathlib

file = pathlib.Path(“guru99.txt”)

if file.exists ():

    print (“File exist”)

else:

    print (“File not exist”)

It will add true or false in front of the “file exists.” As far as the complete code is concerned, it’s as;

import os

from os import path

def main():

    # Print the name of the OS

    print(os.name)

#Check for item existence and type

print(“Item exists:” + str(path.exists(“guru99.txt”)))

print(“Item is a file: ” + str(path.isfile(“guru99.txt”)))

print(“Item is a directory: ” + str(path.isdir(“guru99.txt”)))

if __name__ == “__main__”:

    main()

The output of this file includes, “item exists,” item is the directory,” and “item is a file” with true or file in front of it.

The Bottom Line

With this article, we have rounded methods and modules for Python check if file exists. In addition to file checks, directory existence can be checked as well. Feel free to leave a comment below for any queries or clarification.

No Comments

Leave a Comment