Javaexercise.com

How to Check Whether a File Exists in Python

To check whether the specified file exists or not, Python provides several built-in functions such as exists(), isfile(), is_file(), etc. In this article, we are checking existing file by using these functions.

We will use the os and pathlib library or module to apply these functions. Let's get started.

Example to check file exists using os.path.isfile() function in Python

The isfile() function is used to validate if the specified path denotes a regular file. It returns True if the file exists, False otherwise. In this example, we first import the os module and then use isfile() function. See the code example below.

# import os module
import os 

"""
Using isfile() function that 
check if it is a file

"""

filename = "abc.txt"
if os.path.isfile(filename):
  print("file exist")
else:
  print("file does not exist")

 

Example to check file exists using os.path.exists() function in Python

This is another example where we are checking whether the file exists. This time, we are using the exists() function that is used to check whether the specified file exists or not. It returns True if the path refers to an existing path or an open file descriptor, False otherwise. It also returns False if the specified path is a broken symbolic link.

The os.path.isfile() and os.path.exists() both can be used to check file but the exists() function returns True in case of directory as well. It means, we can use exists() function to check directory also. 

# import os module
import os

"""
Using exists() function that 
checks whether the specified path exists

"""

filename = "abc.txt"
if os.path.exists(filename):
  print("file exist")
else:
  print("file does not exist")

 

Example to check file exists using pathlib.Path.exists() function in Python

Here, we are using a different library pathlib that provides exists() function. We will use this function to check whether the file exists or not. However, this function is used to check whether the path points to an existing file or directory. It returns True if the specified path belongs to a file or directory, False otherwise. See the code example below.

# import pathlib module
from pathlib import Path

"""
Using Path().exists() function that 
checks whether the specified path exists

"""

filename = "abc.txt"
if Path(filename).exists():
  print("file exist")
else:
  print("file does not exist")

 

Example to check file exists using pathlib.Path.is_file() function in Python

Here, we are using the is_file() function that returns True if the path or a symbolic link points to a regular file, False otherwise. It also returns False if the path doesn’t exist or symlink is broken. See the code example below.

# import pathlib module
from pathlib import Path

"""
Using Path().is_file() function that 
checks whether the specified path points to a regular file

"""

filename = "abc.txt"
if Path(filename).is_file():
  print("file exist")
else:
  print("file does not exist")

 

Conclusion

In the end, we have learned to check whether a file exists or not by using python built-in functions such as isfile(), exists(), is_file(), etc. Hope you get the solution.

 

References: