How To Check If A File Or Directory Exists Using Python

You might be writing a Python script and want to create a file if a particular directory exists or not. Python can help search for this using the OS module which assists to interact with the operating system.

In this blog, we will share with you how you can add Python commands to check if a directory exists or not. That said if you want to know how to create a directory or file using Python we have a blog that you can read here.

Having a command to check for a directory can help us avoid prevent overwriting to the already existing file, or maybe make sure that the file is available or not before loading it. 

The following ways can help you check if the file or directory exists or not:

  • Using os.path.exists() 
  • Using os.path.isfile() 
  • Using os.path.isdir() 
  • Using pathlib.Path.exists() 

1. Python: Check if directory exists using os.path.exists() function

os.path.exists() helps check if a specified path exists or not. It returns true if the path is a file, directory, or any other file that contains a reference to another file.

It takes a path as an argument and returns a boolean based on the existence of the directory or path. This command can find both the directory and files.

 os.path.exists(path)
#import the OS module
 import os  
 
#Specify the file path
 path = 'User/techstricks/'
 
#Check whether the specified
 path exists or not
 doesExist = os.path.exists(path)
 print(doesExist)
 
#Specify path to check whether the specified file exists or not
 path = '/User/Documents/program_file.txt'
 
 doesExist = os.path.exists(path)
 print(doesExist)
True  
False

2. Using os.path.isfile() 

Like the command’s name suggests it helps a program find a file if it exists or not. It helps to find whether the given path is a regular file or not. The command returns a boolean based on the existence of a regular file.

os.path.isfile(path)

Similar to the path.exists() this command takes a path as an argument.

#Import the os module to access the operating system
 import os

# Check whether the specified path is an existing file
 File = os.path.isfile('Techstricks/Programs/files/python_file.txt')
 print(File)

#Check whether the specified path is an existing file
 File = os.path.isfile('/home/User/Documents/')
 print(File)
False
True

3. Using os.path.isdir() 

os.path.isdir() helps a program find a directory if it exists or not. It helps to find whether the given path is a directory or not. The command returns a boolean based on the existence of a directory. Like path.isfile() command path.isdir() also follows a symlink to a file/directory.

os.path.isdir(path)

It takes a path as an argument.

#import the os.path module
 import os.path
 
#Check whether the specified path is an existing file or not
 dir = os.path.isdir('/Techstricks/Documents/modules.txt')
 print(dir)
 
#Check whether the specified path is an existing directory or not
 dir = os.path.isdir('/Techstricks/Documents/Python/')
 print(dir)
True
False

Using pathlib.Path.exists()

Python 3.4 and above versions have a pathlib Module to handle the file systems, which uses an object-oriented approach to check whether a file exists or not. The module comes under Python’s standard utility modules and it provides various classes representing file system paths based on various operating system-specific semantics.

pathlib.Path.exists(path)

Following is an example of how you can use the pathlib module:

#import path class from pathlib
from pathlib import Path

 if Path('pytutorial.txt').is_file():
     print ("File exist")
 else:
     print ("File not exist")
File exist

Above were some of the most used methods to check if a file or directory exists or not using python.

If you have any questions or suggestions feel free to comment below.

We will be happy to hear your thoughts

      Leave a reply

      Techs Tricks
      Logo
      Reset Password
      Shopping cart