Different methods to check file exists in Python
When operating with files, you will always need to check for the existence of file before using it for reading from or writing to them. While working on any applications associated with the file, it is required that all the files must be present to proceed further. So, we can use functions from inbuilt python modules like os.path
and pathlib
to check file exists or not. There are four different ways to check for the existence of file in python.
- Using
os.path.exists()
function - Using
os.path.isfile()
- Using the
is_file()
ofpathlib
module - Using
os.path.islink()
to check file exists and is a symbolic link
Method-1: Using os.path.exists() function
The os.path module is the path module that takes some useful function on pathnames. This module is suitable for the operating system Python is running on, and therefore usable for local paths. Here, we will first need to import os.path module.
The syntax of exists method is as shown below. Here, path is a file name or absolute path.
os.path.exists(path)
This function returns True if a given path refers to an existing path or an open file descriptor. However, it will return False value if the given path is referring to a broken symbolic links. Moreover, on some platforms, this function may return False if the permission is not granted to execute os.stat() on the requested file, even if the path physically exists.
Example 1 In this example, we will assume that file demo.txt exists in the same folder as python script.
# Import os.path to use its functions
import os.path
# Using exists method to check the presence of file
fe=os.path.exists("demo.txt")
print("Does demo.txt exists",fe)
Output
Does demo.txt exists True
Example 2 In this example, we will assume that file if exists lies in the different folder.
# Import os.path to use its functions
import os.path
# Using exists method to check the presence of file
fe=os.path.exists("/pythondemo/demo.txt")
print("Does demo.txt exists",fe)
Output
Does demo.txt exists False
Method-2: Using os.path.isfile() function
One more method of os.path module can be used to check for the existence of file. Here also, we will first need to import os.path module. The syntax of isfile() method is as shown below. Here, path is a file name or absolute path.
os.path.isfile(path)
This function returns True if path refers to an existing regular file. This follows symbolic links, so both islink() and isfile() can be true for the same path.
Example 1 In this example, we will assume that file demo.txt exists in the same folder as python script.
# Import os.path to use its functions
import os.path
# Using isfile method to check the presence of file
fe=os.path.isfile("demo.txt")
print("Does demo.txt exists",fe)
Output
Does demo.txt exists True
Example 2 In this example, we will assume that file if exists lies in the different folder.
# Import os.path to use its functions
import os.path
# Using isfile method to check the presence of file
fe=os.path.isfile("/pythondemo/demo.txt")
print("Does demo.txt exists",fe)
Output
Does demo.txt exists False
Method-3: Using the pathlib module
The pathlib
module manipulates files and folders using the object-oriented approach. Firstly, we will need to import the Path class from the pathlib
module. Thereafter, create a new instance of Path class and initialize it with the file path the existence of which we are checking for.
The method is_file() is then used to check if the file exists. The syntax of is_file()
is as given below.
is_file()
This method will return True if the path points to a regular file or a symbolic link pointing to a regular file, and False otherwise. Moreover, it may also return False, if the path doesn’t exist or is a broken symbolic link.
Note that from version 3.8 it returns False if the file doesn’t exist on given path. But, in the earlier versions, it will raise an OSError Exception if the file is not found.
Example 1 In this example, we will assume that file if exists lies in the same folder as python script.
# Import Path from pathlib module
from pathlib import Path
# Check if file exist
path = Path("Demo.txt")
print("Does demo.txt exists ?",path.is_file())
Output
Does demo.txt exists True
Example 2 In this example, we will assume that file if exists lies in the different folder from python script.
# Import Path from pathlib module
from pathlib import Path
# Check if file exist
path = Path("/pythondemo/Demo.txt")
print("Does demo.txt exists ?",path.is_file())
Output
Does demo.txt exists False
Method-4: Using os.path.islink() function to check file exists and is a symbolic link
One more method of os.path module can be used to check if the given file path is a symbolic link also referred as a shortcut to a file on windows system. Here also, we will first need to import os.path module. The syntax of islink() method is as shown below. Here, path is a file name or absolute path.
os.path.islink(path)
This function returns True if path refers to an existing file or directory entry that is a symbolic link and False otherwise.
Note that you will need to create a symbolic link of file (Shortcut of a file) in order to demonstrate the working of islink()
function.
Example 1 In this example, if you are working on windows system, you can right click on the file and create its shortcut within the same folder. However, if you are working on linux system, first create a shortcut of file demo.txt using ln command as shown below.
ln -s '/pythondemo/demo.txt' '/pythondemo/demo(shortcut).txt'
Next we create our code to check if file exists and is a symbolic link:
# Import os.path to use its functions
import os.path
# Using islink method to check if file is symbolic link or not
fe=os.path.islink("/pythondemo/demo(shortcut).txt")
print("Does demo.txt exists",fe)
Output
Does demo.txt exists True
Summary
The knowledge of checking the existence of file is important if we are performing any file operations in real time applications. While implementing any real time systems, it is important that we check for the existence of all file used in the system. In this tutorial, we covered the three different ways to check file exists with an example. All in all, this tutorial, covers everything that you need to know in order to check file exists in Python.
References