How to check if a file exists in Python?

When you perform a file operation such as reading from a file or writing content to a file, we need to check if a file or directory exists before doing the i/o operation.

There are different ways to check if a file exists in Python. Let’s take a look at each one of these in detail.

Python check if a file exists using OS Module

Using the OS module in Python, it’s easy to interact with Operating System. Currently, using OS module methods, we can verify easily if a file or directory exists. 

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

Using os.path.exists()

The os.path.exists() method checks both file and directory, and it returns true if a file or directory exists.

Syntax: os.path.exists(path)

# Example to check if file or directory exists in Python using the OS module
import os


# Output

Using os.path.isfile()

The os.path.isfile() method in Python checks whether the specified path is an existing regular file or not.

Syntax: os.path.isfile(path)

# Example to check if a file exists in Python using the OS module 

import os


# Output

Using os.path.isdir()

The os.path.isdir() method in Python is to check whether the specified path is an existing directory or not. 

Syntax: os.path.isdir(path)

# Example to check if a directory exists in Python using the OS module 

import os


# Output

Using pathlib.Path.exists()

The pathlib module is available in Python 3.4 and above. This module offers object-oriented classes filesystem paths with semantics appropriate for different operating systems.

Pathlib is the modern and most convenient way for almost all file or folder operations in Python, and it is easier to use.

Syntax: pathlib.Path.exists(path)

# Example to check if a file or directory exists in Python using the pathlib module 

from pathlib import Path

file = Path("C:\Projects\Tryouts\etc\password.txt")
if file.exists ():
    print ("File exist")
    print ("File not exist")

# Output
File exist
Leave a Reply

Your email address will not be published.

Sign Up for Our Newsletters

Get notified of the best deals on our WordPress themes.

You May Also Like
Python String Isspace()

Python String isspace()

Table of Contents Hide What are whitespace characters in Python?isspace() Syntaxisspace() Parametersisspace() Return valueExample 1: Working with isspace() method Example 2: Program to check if the string has only whitespace…
View Post
Python List Count()

Python List count()

Table of Contents Hide Syntax of List count() count() ParametersReturn Value from List count()Example 1: Use of count()Example 2: Count Tuple and List Elements Inside List Python List count() is a built-in…
View Post
Python Slice()

Python slice()

Table of Contents Hide slice() Syntax slice() Parametersslice() Return ValueExample 1: Python slice string Get substring using slice objectExample 2: Get substring using negative indexExample 3: Python slice list or Python slice arrayExample 4: Python…
View Post